0

I am creating a client management system in django.

I have two models called 'Client' and 'Installment'. my 'models.py' file is given below:

models.py


from django.db import models
from django.utils import timezone
from django.urls import reverse
# Create your models here.


class Client(models.Model):
    name = models.CharField(max_length = 100)
    dob = models.SlugField(max_length = 100)
    CNIC = models.SlugField(max_length = 100)
    property_type = models.CharField(max_length = 100)
    down_payment = models.IntegerField()
    date_posted = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.name


    def get_absolute_url(self):
        return reverse('client_details',kwargs={ 'pk' : self.pk})

class Installment(models.Model):
    client = models.ForeignKey(Client, blank=True, on_delete=models.CASCADE)
    installment_month = models.CharField(max_length = 100)
    installment_amount = models.IntegerField()
    installment_date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.installment_month

    def get_absolute_url(self):
        return reverse('installment_confirmation')


I am using ForeignKey in the installment model to link the Client model to it, because one client can have multiple installments.

When I RUN the following two commands

python manage.py makemigrations
python manage.py migrate

I didn't get any error.

Then I RUN the server using:

python manage.py runserver

and it runs successfully.

Then I add a new client in My app that uses client model.

But when I want to add installment to the client, is takes input from the user in the fields (installment_month and installment_month), and when I click 'add installment', it gives the following error:

IntegrityError at /1/Add-Installment
NOT NULL constraint failed: property_details_installment.client_id
Request Method: POST
Request URL:    http://127.0.0.1:8000/1/Add-Installment
Django Version: 2.1.3
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: property_details_installment.client_id
Exception Location: /anaconda3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 296
Python Executable:  /anaconda3/bin/python3
Python Version: 3.6.5
Python Path:    
['/Users/razajaved/Documents/installment_plan',
 '/anaconda3/lib/python36.zip',
 '/anaconda3/lib/python3.6',
 '/anaconda3/lib/python3.6/lib-dynload',
 '/anaconda3/lib/python3.6/site-packages',
 '/anaconda3/lib/python3.6/site-packages/aeosa']
Server time:    Sun, 28 Mar 2021 13:55:36 +0000

What I tried next is that I removed the 'blank=True' parameter from the ForeignKey line in the installment model. and then it adds the installment successfully.

But, to check that the added installment is correctly corresponds to the added client. I went into the shell using the following command:

python manage.py shell

Once in the shell i did the following:

from property_details.models import Client
from property_details.models import Installment

Then I tried to check the client I added using the following command:

client1 = Client.objects.filter('clientName')

It works.

I can also check the added installment using the above command.

But when I RUN

client1.id

it gives the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-abd51507c2e0> in <module>()
----> 1 client1.id

AttributeError: 'QuerySet' object has no attribute 'id'

But when I RUN

installment.id

it returns the id of the added installment.

It looks like, there is some problem with the ForeignKey I used in the installment model, and there is no id assigned to the client. I have tried different solution like putting 'blank=True' OR 'null=True' parameters. But it didn't work for me.

Does anyone has an idea how to solve this problem?

1 Answer 1

1

one error what I can see is this:

client1 = Client.objects.filter('clientName')

it should be like this:

client1 = Client.objects.filter('clientName').first()

filter() returns QuerySet you have to get the model instance either by first() or slicing then only you can access the fields like client1.id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.