django表单返回无法赋值"'1'":“主要.album”必须是“主要相册”示例

aamkag61  于 2022-12-30  发布在  Go
关注(0)|答案(1)|浏览(170)

我在学校数据库工作,当我尝试使用纯html表单提交我的django表单时,它抛出了这样的错误:ValueError at /primary Cannot assign "'1'": "Primary.album" must be a "PrimaryAlbum" instance.我该如何解决这个错误?
我第一次使用这个方法:

class PrimaryCreativeView(LoginRequiredMixin, CreateView):
    model = Primary
    fields = ['profilePicture', 'firstName', 'sureName', 
    'gender', 'address', 'classOf', 'album', 
    'hobbies', 'dateOfBirth','year', 
    'name', 'contact', 'homeAddress', 
    'emails', 'nationality','occupations', 
    'officeAddress', 'state', 'localGovernments', 
    'relationship' ]
    template_name = 'post_student_primary.html'
    success_url = reverse_lazy('home')

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super (PrimaryCreativeView, self).form_valid(form)

所以我把它改成了下面这个方法,但是我不知道为什么它会抛出这个错误:ValueError at /primary Cannot assign "'1'": "Primary.album" must be a "PrimaryAlbum" instance.当我提交表单时。我该如何解决?为什么显示此错误?
我的意见:

def primary_submit_form(request):
    albums = PrimaryAlbum.objects.filter(user=request.user)

    if request.method == 'POST':
        addmisssion_number = request.POST.get('addmisssion_number')
        profile_picture = request.POST.get('profile_picture', None)
        first_name = request.POST.get('first_name')
        sure_name = request.POST['sure_name']
        gender = request.POST['gender']
        address_of_student = request.POST['address_of_student']
        class_Of_student = request.POST['class_Of_student']
        album = request.POST['album']
        date_of_birth = request.POST['date_of_birth']
        nationality_of_student = request.POST['nationality_of_student']
        state_of_student = request.POST['state_of_student']
        local_government_of_student = request.POST['local_government_of_student']
        certificate_of_birth_photo = request.FILES.get('certificate_of_birth_photo')
        residential_certificate_photo = request.FILES.get('residential_certificate_photo')
        

        name = request.POST['name']
        contact = request.POST['contact']
        address_2 = request.POST['address_2']
        nationality_2 = request.POST['nationality_2']
        occupations = request.POST['occupations']
        work_address = request.POST['work_address']
        state_2 = request.POST['state_2']
        local_government_2 = request.POST['local_government_2']
        relationship = request.POST['relationship']

        student_create = Primary.objects.create(
            addmisssion_number=addmisssion_number, profile_picture=profile_picture,
            first_name=first_name, sure_name=sure_name, gender=gender, address_of_student=address_of_student,
            class_Of_student=class_Of_student, album=album, date_of_birth=date_of_birth,
            nationality_of_student=nationality_of_student, state_of_student=state_of_student, local_government_of_student=local_government_of_student,
            certificate_of_birth_photo=certificate_of_birth_photo, residential_certificate_photo=residential_certificate_photo,

            name=name, contact=contact, address_2=address_2, nationality_2=nationality_2, occupations=occupations, work_address=work_address,
            state_2=state_2, local_government_2=local_government_2, relationship=relationship
        )
        student_create.save()

        return redirect('Primary-Albums')
    return render(request, 'create_primary_student_information.html', {'albums':albums})

我的模特:

class PrimaryAlbum(models.Model):
    name = models.CharField(max_length=100)
    user = models.ForeignKey(User,
     on_delete=models.CASCADE)

    def __str__(self):
        return self.name       

class Primary(models.Model):
    addminssion_number = models.CharField(max_length=40)
    profilePicture = models.FileField(upload_to='image')
    first_name = models.CharField(max_length=40) 
    sure_name = models.CharField(max_length=40) 
    gender = models.CharField(max_length=25)  
    address_of_student = models.CharField(max_length=50)
    class_Of_student = models.CharField(max_length=20)
    date_of_birth = models.CharField(max_length=20)
    album = models.ForeignKey(PrimaryAlbum, on_delete=models.CASCADE)
    hobbies = models.TextField(blank=True, null=True)
    nationality_of_student = models.CharField(max_length=50)
    state_of_student = models.CharField(max_length=30)
    local_government_of_student = models.CharField(max_length=50)
    certificate_of_birth_photo = models.FileField(upload_to='image')
    residential_certificate_photo = models.FileField(upload_to='image')

    #Guadians
    name = models.CharField(max_length=30)
    contact = models.CharField(max_length=15)
    address_2 = models.CharField(max_length=50)
    nationality_2 = models.CharField(max_length=50)
    occupations = models.CharField(max_length=50)
    work_address = models.CharField(max_length=50)
    state = models.CharField(max_length=30)
    local_government = models.CharField(max_length=50)
    relationship = models.CharField(max_length=25)

    def __str__(self):
        return str(self.first_name)

我的模板:

<form action="{% url 'Create-Primary-Student' %}" method="POST">
        {% csrf_token %}
        <div class="container">
            <div class="row">
                <div class="text-center">
                    {% for message in messages %}
                    <h5>{{message}}</h5>
                    {% endfor %}
                </div>
                <div class="col-md-4">
                    <label for="" class="form-label">Admission Number</label>
                    <input type="text" class="form-control" name="addmission_number">
                    <br>
                </div>
            
                <div class="col-md-4">
                    <label for="" class="form-label">Profile Picture</label>
                    <input type="file" class="form-control" name="profile_picture">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">First Name</label>
                    <input type="text" class="form-control" name="first_name">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Sure Name</label>
                    <input type="text" class="form-control" name="sure_name">
                    <br>
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label"> Gender</label>
                    <input type="text" class="form-control" name="gender">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Address</label>
                    <input type="text" class="form-control" name="address_of_student">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Class</label>
                    <input type="text" class="form-control" name="class_Of_student">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Date Of Birth</label>
                    <input type="date" class="form-control" name="date_of_birth">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-group">Year Of Graduation</label>
                    <select class="form-select" aria-label="Default select example" name="album">
                        <option selected>Select Year Of Graduation</option>
                        {% for album in albums %}
                        <option value="{{ album.id }}">{{ album.name }}</option>
                        {% endfor %}
                      </select>
                      <br>
                </div>

                <div class="col-md-4">
                    <label class="form-label">Example textarea</label>
                    <textarea class="form-control" rows="3" name="hobbies"></textarea>
                    <br>
                </div>

                <div class="col-md-4">
                    <br>
                    <label class="form-label">Student Country</label>
                    <input type="text" class="form-control" name="nationality_of_student">
                </div>

                <div class="col-md-4">
                    <br>
                    <label for="" class="form-label">State Of Origin</label>
                    <input type="text" class="form-control" name="state_of_student">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Local Government</label>
                    <input type="text" class="form-control" name="local_government_of_student">
                    <br>
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Certificate of Birth Photo</label>
                    <input type="file" class="form-control" name="certificate_of_birth_photo">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Student Indigen Photo</label>
                    <input type="file" class="form-control" name="residential_certificate_photo">
                </div>

                <div class="text-center">
                    <br>
                    <h2>Guidance Information</h2>
                    <br>
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Full Name</label>
                    <input type="text" class="form-control" name="name">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Phone Number</label>
                    <input type="text" class="form-control" name="contact">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Home Address</label>
                    <input type="text" class="form-control" name="address_2">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Country</label>
                    <input type="text" class="form-control" name="nationality_2">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Occupation</label>
                    <input type="text" class="form-control" name="occupations">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Work Address</label>
                    <input type="text" class="form-control" name="work_address">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">State Of Origin</label>
                    <input type="text" class="form-control" name="state_2">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Local Government</label>
                    <input type="text" class="form-control" name="local_government_2">
                </div>

                <div class="col-md-4">
                    <label for="" class="form-label">Relationship To Student</label>
                    <input type="text" class="form-control" name="relationship">
                </div>

                <button type="submit" class="btn btn-success">create album</button>

            </div>
        </div>
    </form>
k3bvogb1

k3bvogb11#

您可以通过将其赋值给album_id而不是album来创建它:

student_create = Primary.objects.create(
    addmisssion_number=addmisssion_number,
    profile_picture=profile_picture,
    first_name=first_name,
    sure_name=sure_name,
    gender=gender,
    address_of_student=address_of_student,
    class_Of_student=class_Of_student,
    album_id=album,
    date_of_birth=date_of_birth,
    nationality_of_student=nationality_of_student,
    state_of_student=state_of_student,
    local_government_of_student=local_government_of_student,
    certificate_of_birth_photo=certificate_of_birth_photo,
    residential_certificate_photo=residential_certificate_photo,
    name=name,
    contact=contact,
    address_2=address_2,
    nationality_2=nationality_2,
    occupations=occupations,
    work_address=work_address,
    state_2=state_2,
    local_government_2=local_government_2,
    relationship=relationship,
)

然而,我强烈建议使用ModelForm [Django-doc],这将从视图中删除大量的样板代码,进行适当的验证,并有效地创建对象。

相关问题