Home > Software design >  django form return Cannot assign "'1'": "Primary.album" must be a &quo
django form return Cannot assign "'1'": "Primary.album" must be a &quo

Time:12-28

Am working on school database, when I'm trying to submit my django form using pure html form, it throws me an error like this: ValueError at /primary Cannot assign "'1'": "Primary.album" must be a "PrimaryAlbum" instance. How can i solve this error please?

Am using this method for the first time:

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)

so I changed it to this method down below, but I don't know why it throws me this error:ValueError at /primary Cannot assign "'1'": "Primary.album" must be a "PrimaryAlbum" instance. when I submitted the form. How can I solve? And why this error is shown?

my Views:

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})

my models:

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)

My Templates:


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

                <div >
                    <label for="" >First Name</label>
                    <input type="text"  name="first_name">
                </div>

                <div >
                    <label for="" >Sure Name</label>
                    <input type="text"  name="sure_name">
                    <br>
                </div>

                <div >
                    <label for="" > Gender</label>
                    <input type="text"  name="gender">
                </div>

                <div >
                    <label for="" >Student Address</label>
                    <input type="text"  name="address_of_student">
                </div>

                <div >
                    <label for="" >Student Class</label>
                    <input type="text"  name="class_Of_student">
                </div>

                <div >
                    <label for="" >Student Date Of Birth</label>
                    <input type="date"  name="date_of_birth">
                </div>

                <div >
                    <label for="" >Year Of Graduation</label>
                    <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 >
                    <label >Example textarea</label>
                    <textarea  rows="3" name="hobbies"></textarea>
                    <br>
                </div>

                <div >
                    <br>
                    <label >Student Country</label>
                    <input type="text"  name="nationality_of_student">
                </div>

                <div >
                    <br>
                    <label for="" >State Of Origin</label>
                    <input type="text"  name="state_of_student">
                </div>

                <div >
                    <label for="" >Student Local Government</label>
                    <input type="text"  name="local_government_of_student">
                    <br>
                </div>

                <div >
                    <label for="" >Student Certificate of Birth Photo</label>
                    <input type="file"  name="certificate_of_birth_photo">
                </div>

                <div >
                    <label for="" >Student Indigen Photo</label>
                    <input type="file"  name="residential_certificate_photo">
                </div>

                <div >
                    <br>
                    <h2>Guidance Information</h2>
                    <br>
                </div>

                <div >
                    <label for="" >Full Name</label>
                    <input type="text"  name="name">
                </div>

                <div >
                    <label for="" >Phone Number</label>
                    <input type="text"  name="contact">
                </div>

                <div >
                    <label for="" >Home Address</label>
                    <input type="text"  name="address_2">
                </div>

                <div >
                    <label for="" >Country</label>
                    <input type="text"  name="nationality_2">
                </div>

                <div >
                    <label for="" >Occupation</label>
                    <input type="text"  name="occupations">
                </div>

                <div >
                    <label for="" >Work Address</label>
                    <input type="text"  name="work_address">
                </div>

                <div >
                    <label for="" >State Of Origin</label>
                    <input type="text"  name="state_2">
                </div>

                <div >
                    <label for="" >Local Government</label>
                    <input type="text"  name="local_government_2">
                </div>

                <div >
                    <label for="" >Relationship To Student</label>
                    <input type="text"  name="relationship">
                </div>

                <button type="submit" >create album</button>


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

CodePudding user response:

You create this by assigning it to album_id, not 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,
)

I would however strongly advise to use a ModelForm [Django-doc] this will remove a lot of boilerplate code from the view, do proper validation, and create the object effectively.

  • Related