Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model field check instance

I have a models like

class MyModel(models.Model):
    name = models.CharField()
    updated_on = models.DateTimeField()

Now I am querying this like

m = MyModel.objets.get(id=2)

Here I want to check if the field is of certain type I want to do some stuff like

from django.db.models import DateTimeField

if isinstance(m.updated_on, DateTimeField):
    // do something

But here I am getting false. Is there any workaround by which I can achieve this ??

like image 687
varad Avatar asked Aug 22 '26 19:08

varad


1 Answers

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): ...

like image 76
Deepam Patel Avatar answered Aug 25 '26 09:08

Deepam Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!