Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:36:11+00:00 2026-06-12T12:36:11+00:00

This is my model: class People(models.Model): name = models.CharField(max_length=100) lastname = models.CharField(max_length=100) In views.py

  • 0

This is my model:

class People(models.Model):
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)

In views.py

-When I try this one to count empty lastname fields:

People.objects.filter(lastname__isnull=True).count()

It returns always 0 event though there are some empty lastname fields.

Why I am getting 0, is there any problem with my code or Is there any other way to count empty fields in a table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:36:12+00:00Added an answer on June 12, 2026 at 12:36 pm

    Your CharField is most likely storing empty strings, not NULL

    Django docs on null field value

    Field.null
    If True, Django will store empty values as NULL in the
    database. Default is False.

    Note that empty string values will always get stored as empty strings,
    not as NULL. Only use null=True for non-string fields such as
    integers, booleans and dates. For both types of fields, you will also
    need to set blank=True if you wish to permit empty values in forms, as
    the null parameter only affects database storage (see blank).

    Avoid using null on string-based fields such as CharField and
    TextField unless you have an excellent reason. If a string-based field
    has null=True, that means it has two possible values for “no data”:
    NULL, and the empty string. In most cases, it’s redundant to have two
    possible values for “no data;” Django convention is to use the empty
    string, not NULL.

    Try changing your query to:

    People.objects.filter(lastname="").count()
    

    Empty is not the same as NULL. You would have needed to set your fields to have null=True, which as the docs suggest, is not recommended for a CharField.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have next model: class People(models.Model): name = models.CharField(max_length=100) lastname = models.CharField(max_length=100) In views.py
I have next model: class People(models.Model): name = models.CharField(max_length=100) lastname = models.CharField(max_length=100) class Salary(models.Model):
I have this model: class People(models.Model): name = models.CharField(max_length=128, db_index=True) friends = models.ManyToManyField('self') So
I have this model: class Journals(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code, max_length=50) name
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,
This is my model class UserAward(models.Model): user = models.ForeignKey(User, related_name='awards') award = models.CharField(max_length=255) week
This is my model: class Position(models.Model): map = models.ForeignKey(Map,primary_key=True) #members=models.CharField(max_length=200) LatLng = models.CharField(max_length=40000) infowindow
Hay, I have a Model which looks like this class Person(models.Model): name = models.CharField(blank=False,
I've this models: class Person(models.Model): name = models.CharField() likes = models.ManyToManyField(Image) class Image(models.Model): name
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.