DateTimeField queryset returning None in Django

Willy G

I am trying to create a queryset for getting the values of a DateTimeField which is DATETIME in the DB.

The class in models.py:

class ChangeMetrics(models.Model):
    id = models.IntegerField(primary_key=True)
    file_id = models.ForeignKey(File, db_column = 'file_id')
    version_id = models.ForeignKey(Version, db_column = 'version_id')
    function_id = models.ForeignKey(Function, blank=True, db_column = 'function_id')
    date = models.DateTimeField(blank=True, null=True)
    user = models.TextField(blank=True)
    changed = models.IntegerField(blank=True, null=True)

The field in the DB:

date DATETIME

The tuples are populated in the database and running SQL queries directly on the DB is working perfectly.

This is the queryset I am currently using in Django:

queryset = ChangeMetrics.objects.filter(~Q(changed=None), ~Q(date=None), ~Q(version_id=None))

I have tried a raw query and also a version of the query that uses exclude(), but that still returns None for date.

I am accessing the entries in the queryset through a for loop and simply accessing date through entry.date inside the for loop.

Edit: Django version 1.6.5 I have also tried getting the values through the Django shell, to no success.

Any ideas on what could be wrong?

Alexander Truslow

not sure if you were able to figure this out, but I just had this problem and was able to fix it.

So, Django migrations were creating the column in the DB as datetime(6) instead of datetime. This was causing the Django models to fail to instantiate the Datetime object.

ALTER TABLE `my_table` 
MODIFY COLUMN `created` datetime NOT NULL

After running that if fixed my issue. Maybe in your case you could try modifying to datetime(6) instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

django queryset filter datetimefield

From Dev

How to write Django QuerySet to get UNIX_TIMESTAMP of a DateTimeField?

From Dev

django queryset iterator not returning records in action

From Dev

Django QuerySet slicing returning unexpected results

From Dev

django queryset iterator not returning records in action

From Dev

Django model QuerySet not returning when filtered

From Dev

Filter data from queryset in Django wihtout returning the entire queryset

From Dev

django form is returning None even if something is in the input

From Dev

How to filter a Django QuerySet's related fields' 'all' or 'none'

From Dev

Django, GenericForeignKey is None when accessing an item of QuerySet result?

From Dev

Filtering out duplicate and none values from Django queryset

From Dev

Using a complex custom SQL query and returning a queryset in Django 1.7

From Dev

Django Haystack with elasticsearch returning empty queryset while data exists

From Dev

Django queryset in view isn't returning anything to the html template

From Dev

Using a complex custom SQL query and returning a queryset in Django 1.7

From Dev

Django Haystack with elasticsearch returning empty queryset while data exists

From Dev

Python "is not None" returning None

From Dev

Pull only a JSON serializable part of a DateTimeField into a queryset?

From Dev

Changing DateField to DateTimeField in Django

From Dev

Django DateTimeField default

From Dev

Using DateTimeField in Django with Oracle

From Dev

problems with DateTimeField filter in Django

From Dev

Lookup hour on DateTimeField Django

From Dev

problems with DateTimeField filter in Django

From Dev

Django DateTimeField User Input

From Dev

how to curl datetimefield to django

From Dev

DateTimeField is not defined error in django

From Dev

django datetimefield and postgres

From Dev

Django authenticate function returning None when passed existing username and password

Related Related

  1. 1

    django queryset filter datetimefield

  2. 2

    How to write Django QuerySet to get UNIX_TIMESTAMP of a DateTimeField?

  3. 3

    django queryset iterator not returning records in action

  4. 4

    Django QuerySet slicing returning unexpected results

  5. 5

    django queryset iterator not returning records in action

  6. 6

    Django model QuerySet not returning when filtered

  7. 7

    Filter data from queryset in Django wihtout returning the entire queryset

  8. 8

    django form is returning None even if something is in the input

  9. 9

    How to filter a Django QuerySet's related fields' 'all' or 'none'

  10. 10

    Django, GenericForeignKey is None when accessing an item of QuerySet result?

  11. 11

    Filtering out duplicate and none values from Django queryset

  12. 12

    Using a complex custom SQL query and returning a queryset in Django 1.7

  13. 13

    Django Haystack with elasticsearch returning empty queryset while data exists

  14. 14

    Django queryset in view isn't returning anything to the html template

  15. 15

    Using a complex custom SQL query and returning a queryset in Django 1.7

  16. 16

    Django Haystack with elasticsearch returning empty queryset while data exists

  17. 17

    Python "is not None" returning None

  18. 18

    Pull only a JSON serializable part of a DateTimeField into a queryset?

  19. 19

    Changing DateField to DateTimeField in Django

  20. 20

    Django DateTimeField default

  21. 21

    Using DateTimeField in Django with Oracle

  22. 22

    problems with DateTimeField filter in Django

  23. 23

    Lookup hour on DateTimeField Django

  24. 24

    problems with DateTimeField filter in Django

  25. 25

    Django DateTimeField User Input

  26. 26

    how to curl datetimefield to django

  27. 27

    DateTimeField is not defined error in django

  28. 28

    django datetimefield and postgres

  29. 29

    Django authenticate function returning None when passed existing username and password

HotTag

Archive