0

I'm handling some edge cases in some django code, and i'm hitting a problem that is only encountered when specific fields are involved.

I want to be able to detect the class that the Model is using for a field and depending upon the result drop into alternate logic.

I've tried to use get_internal_type() on the field instance but it just returns "BooleanField" and not the expected "ModifiedField"

The Custom Field Type:

class ModifiedField(models.BooleanField):
    def __init__(self, *args, **kwargs):
        kwargs['editable'] = False
        models.BooleanField.__init__(self, *args, **kwargs)

    def pre_save(self, model_instance, add):
        value = getattr(model_instance, self.attname)

        if add:
            return True
        elif value == 2:
            return False
        else:
            return True

Model:

class TemplateItem(models.Model):
    uuid = UUIDField(primary_key=True)
    name = models.CharField(null=False, blank=True, max_length=255)
    image = models.ImageField(_('Image'), upload_to=_template_image_upload_path,
                              storage=item_fs, null=True, blank=True)
    is_modified = ModifiedField()
Jharwood
  • 1,046
  • 2
  • 11
  • 28

2 Answers2

1

Have you tried class._name_ ??

g = TemplateItem.objects.get(id=1)
g.__class__.__name__
Silwest
  • 1,620
  • 1
  • 15
  • 29
0
fld_cls = type(model_instance._meta.get_field(fieldname))
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118