Skip to content

__isnull lookup doesn't work as expected when null=True #149

Description

@xarillian

On the latest version, "django-timezone-field==7.1", and PostgreSQL

I ran into some unexpected behavior with the __isnull lookup when using null=True on a TimeZoneField. I wanted to check if this is intended behaviour or a bug.

When I filter for non-null timezone values using time_zone__isnull=False, I still get records where time_zone is None:

from django.db import models
from timezone_field import TimeZoneField

class MyModel(models.Model):
    time_zone = TimeZoneField(null=True, blank=True)

# Create a record with None timezone
obj = MyModel.objects.create(time_zone=None)

results = MyModel.objects.filter(time_zone__isnull=False)  # <- This should exclude the record above, but it doesn't

Looking at the code, _get_python_and_db_repr always returns an empty string for the database representation when the value is None, regardless of the null setting:

def _get_python_and_db_repr(self, value):
    if value is None or value == "":
        return (None, "")

Is the current behaviour intentional? There is a workaround using .exclude(time_zone="").

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions