[Django]-Django models: Reference field return multiple type of models

4๐Ÿ‘

โœ…

I think you are looking for a Generic Relationship:

class Property(models.Model):
    user=models.ForeignKey(User)
    base=models.ForeignKey(Base)
    content_type = models.ForeignKey(ContentType) # Which model is `item` representing?
    object_id = models.PositiveIntegerField() # What is its primary key?
    item=generic.GenericForeignKey('content_type', 'object_id') # Easy way to access it.
    amount=models.IntegerField(default=0)
    level=models.SmallIntegerField(default=0)

This lets you create items as you mentioned, however you would probably need to look at a different way of filtering those items out.

๐Ÿ‘คJack M.

Leave a comment