Store different django field types in the same column of a database

I am considering storing image paths, file paths, and char fields in the same column of an SQL table. In django these would be ImageFields, FileFields and CharFields. I would have a column that tells me what type of information is stored so I could handle it correctly.

I would like to take advantage of the django code already written to handle images and files. Would I make a custom field to do this sort of thing?

I think you’d be better off creating the fields as nullable, using a check constraint to ensure only one is set ( https://adamj.eu/tech/2020/03/25/django-check-constraints-one-field-set/ ), and then using an @property to return the value from the correct field using your other “type of informatioin” field.

@property 
def thing(self):
    if self.thing_type == ThingType.IMAGE:
        return self.thing_image_field
    elif...
1 Like