[Django]-Django model field check instance

4👍

To get the type of field:
Model._meta.get_field("fieldname")

You can use this:

from django.db.models import DateTimeField
if isinstance(MyModel._meta.get_field('updated_on'),DateTimeField):
...

0👍

I don’t know why the data in the field would be anything other than what you have defined it as.

But your problem is that the type of the field is not the same as the type of the data, and it’s the latter you care about. The type of data in a DateTimeField is datetime.datetime.

So:

from datetime import datetime
if isinstance(m.updated_on, datetime):
    ...

Leave a comment