Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - Field 'id' expected a number but got 'Student'

I tried to apply python manage.py migrate I get the error Field 'id' expected a number but got 'Student'.

enter image description here

I've set the value of primary key to be id. views.py:

#----------------------STUDENT OPERATION------------------------------------

    @login_required()
    def test_form(request):
       students = TestModel.objects.all()
       paginator = Paginator(students,20)
       page_number = request.GET.get('pages')
       page_obj = paginator.get_page(page_number)
    
        enter code here
    
       if request.method == 'POST':
            form = TestForm(request.POST)
            if form.is_valid():
                x = form.instance.student
                print(x)
                p = form.save(commit=False)
                p.save()
                messages.success(request,'Student "{}" has been succesfully added!'.format(x))
                return redirect('testform')
       else:
            form = TestForm()
       return render(request,'testform.html',  {'form':form,'students':page_obj})
    
    
    @login_required()
    def update_form(request,id):
        if request.method == 'POST': #defpost
            obj = TestModel.objects.get(pk = id)
            form = TestForm(request.POST,instance=obj)
            if form.is_valid():
                form.save()
                messages.success(request,'Student "{}" was succesfully updated'.format(obj.student))
                return redirect('testform')
        else: #def get()
            obj = TestModel.objects.get(pk=id)
            print(obj.student)
            print('###')
            form = TestForm(instance=obj)
    
        return render(request,'testform_update.html',{'form':form})
    
    @login_required()
    def del_testform(request,id):
        if request.method == 'POST':
            obj = TestModel.objects.get(pk = id)
            student = obj.student
            obj.delete()
            messages.warning(request,'Student "{}" has been deleted succesfully!'.format(student))
        return redirect('testform')
    
    
    def home(request):
        posts = Post.objects.all().order_by('-date_posted')[:8]
        destinations = Destinations.objects.all().order_by('date_posted')[:6]
        return render(request,'home.html',  {'posts':posts, 'destinations':destinations})
    

models.py:

    class TestModel(models.Model):
        GENDER_CHOICES = (('Male','Male'),('Female','Female'))
        student = models.CharField(max_length=100,null=True)
        address = models.CharField(max_length=100)
        gender = models.CharField(choices=GENDER_CHOICES,max_length=50)
        email = models.EmailField(null=True)
        def __str__(self):
            return self.student

testform.html:

        {%  extends 'index.html' %}
    {% load static %}
    {% load crispy_forms_tags %}
    
    {% block content %}
    <div class="container-fluid">
        <div class="row justify-content-center">
            <div class="col-md-8">
                {% if messages %}
                {% for message in messages %}
                <div class="alert alert-{{ message.tags }} alert-dismissible fade show my-2" role="alert">
                    {{ message }}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                {% endfor %}
                {% endif %}
                <div class="col-md-5 mr-auto">
                    <form  method="POST">
                    {% csrf_token %}
                    <legend class="">Enter Student Details</legend>
    
    
                    <div class="form-group">
                        {{form|crispy}}
                        <button type="submit" class="btn btn-primary">Add Student</button>
                    </div>
                </form>
                </div>
                    <div>
    
            </div>
                <div class="my-2 ">
                    <div class="text-center border bg-white">
                        <h3>Student Table</h3>
                    </div>
                    <table class="table  table-white table-striped   table-hover table-bordered">
                         <tr>
                            <td colspan="6">
                                <form class="form-inline" name = "table-search" >
                                    <div class="form-group mr-auto">
                                        <input type="text" class="form-control" placeholder = "Search Student">
                                           <button class="btn btn-primary ml-2 mt-1 form-control  " type="submit">Search</button>
                                    </div>
                                    <div class="form-group">
    
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <thead>
                        <tr>
                            <th>SN</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Gender</th>
                            <th>Email</th>
    
                            <th>Action</th>
    
                        </tr>
                        </thead>
    
                        {% for student in students %}
                        <tr>
                            <td>{{forloop.counter}}.</td>
                            <td>{{student.student}}</td>
                            <td>{{ student.address}}</td>
                            <td>{{student.gender}}</td>
                            {% if student.email %}
                             <td>{{student.email}}</td>
                            {% elif student.email == None %}
                            <td class="text-danger">Not Available</td>
                            {% endif %}
    
                            <td>
                                <button type="button" class="btn btn-danger btn-sm" data-toggle="modal"
                                        data-target="#staticBackdrop{{student.id}}">
                                   <i class="fas fa-trash mr-2"></i> Delete
                                </button>
                                <a class="btn btn-primary btn-sm    " href="{% url 'testform-update' student.id%}"><i class="fas fa-pen mr-2"></i> Update</a>
    
                            </td>
                        </tr>
                         <!-- modal-->
                                <div class="modal fade" id="staticBackdrop{{student.id}}" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-dialog-centered">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="staticBackdropLabel"><span class="text-danger">Delete</span></h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <div class="text-muted">Delete student <strong>"{{student.student}}"</strong>?
                                            </div>
                                            <div class="modal-footer">
                                                <form method="POST"  action="{% url 'testform-delete' student.id %}" name="deleteform">
                                                    {% csrf_token %}
                                                    <button class="btn btn-danger" type="submit">Delete</button>
                                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close
                                                </button>
                                                </form>
    
    
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <!-- modal-->
    
                        {% endfor %}
                    </table>
    
                </div>
            </div
        </div>
    </div>
    
    {% endblock %}
question from:https://stackoverflow.com/questions/65886077/field-id-expected-a-number-but-got-student

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...