I am trying to create a django model which has as one of its fields a reference to some sort of python type, which could be either a integer, string, date, or decimal.
class MyTag(models.Model):
name = models.CharField(max_length=50)
object = (what goes here??)
I know that if I want a foreign key to any other model, I can use GenericForeignKeys and content_types. How can I have a model field that references any python type? The only idea I have come up with so far is to create models that are simple wrappers on objects and use GenericForeignKeys.
Is there any way to do this?
Since you want to filter, you would need some kind of DB support for your field and json field won’t be that valuable for you. You can use some different solution with different
complication levels according to your actual need. One suggestion is to serialize your data to string. Pad with enough zeros for string/integer/float sorting. Now you can filter all your stuff as string (make sure you pad the value you are filtering by as well). Add a data_type column for fetching the right python object.
(Disclaimer: this is a hack, but probably not the worst one).