Do not filter any django fields

I have a Person model that sometimes has nothing in the birth_date field. Here is an example:

 (Pdb) dude = Person.objects.get(pk=20) (Pdb) dude <Person: Bob Mandene> (Pdb) dude.birth_date (Pdb) dude.birth_date == None True 

How to filter these entries with birth_date == None ?

I have already tried the following without success:

1: "birth_date__isnull" does not work.

  Person.objects.filter(birth_date__isnull = True) # does not return the required # object. Returns a record that have birth_date set to # NULL when the table was observed in MySQL. 

The record with identifier 20 is not returned below.

 Person.objects.filter(birth_date = "") 

How to filter in the None field? NULL and None seem to be different. When I see data using the pro sequel (mysql graphical client), I see "0000-00-00 00:00:00", and the following does not work either

 (Pdb) ab=Patient.objects.filter(birth_date = "0000-00-00 00:00:00") ValidationError: [u"'0000-00-00 00:00:00' value has the correct format (YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time."] 

model

 class Person(models.Model): person_no = models.IntegerField(primary_key=True) locationid = models.IntegerField(null=True, db_column='LocationID', blank=True) address = models.ForeignKey('Address', db_column = 'address_no') birth_date = models.DateTimeField(null=True, blank=True) class Meta: managed = False db_table = 'person' 
+4
source share
4 answers

NULL in mysql turns into None in python, so what you had with birth_date__isnull = True should return those that have birth_date as None.

0
source

Hmm, I'm not sure why 0000-00-00 00:00:00 cast to None in this case, but you can try to use exact

 Person.objects.filter(birth_date__exact=None) 
0
source

A little understanding of the list may come in handy here:

 persons = [person for person in Person.objects.all() if not person.birth_date] 
0
source

I have the same problem with Django using Graphene to create the GraphQL API. Isnull filters do not work properly.

There are some answers that would work if I were filtering in code, but with Graphene and GraphQL we want the filters to be processed on the fly (when they are used by the graphql query).

It has been 5 years since this issue was raised. Perhaps my β€œanswer” might stir the pot again!

0
source

Source: https://habr.com/ru/post/1496700/


All Articles