How to fetch data from database django

I have created a CustomerRegistration model and I want that if the phone no exists in the database, then all the details of the customer will display automatically in form.

here is my

class CustomerRegistration(models.Model):
    name = models.CharField(max_length=254, null=False)
    email = models.EmailField(max_length=254, null=False)
    date_of_birth = models.DateTimeField(null=False)
    country_id = models.ForeignKey('accounts.Country', null=False, on_delete=models.CASCADE, related_name='Country')
    state_id = models.ForeignKey('accounts.State', null=False, on_delete=models.CASCADE, related_name='State')
    cities_id = models.ForeignKey('accounts.City', null=False, on_delete=models.CASCADE, related_name='city')
    address = models.CharField(max_length=254, null=False)
    refernce_by_person_name = models.CharField(max_length=254, null=False)
    refernce_by_person_contact_no = models.IntegerField(null=True)
    phone_no = models.IntegerField(null=False, primary_key=True)
    alternate_no = models.IntegerField(null=False)
    hobbies = models.CharField(max_length=254)

    def __str__(self):

2 answers

  • answered 2020-07-11 09:53 zjb

    Have a look at making queries in the django documentation. You will want to make a query to your databse, and if you get any results, populate your form with the correct info.

    This would look something like:

    from .models import CustomerRegistration
    results = CustomerRegistration.objects.all().filter(phone=given_number)

    then, if there are any results, you can use it to populate your form!

  • answered 2020-07-11 10:08 Mohammad Javad Vahediyan

    You can query like this to understand that user has phone number or not or something like that:


    isnull specifies that a field is none or not