Lets say I have these this base model:
class Trackable(PolymorphicModel):
uuid = UUIDField(unique=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
And a child model extends it:
class Like(Trackable):
content = models.ForeignKey(Content, related_name='likes')
class Meta:
unique_together = ['content', 'created_by']
When I run migration, it complains about:
django.db.models.fields.FieldDoesNotExist: Like has no field named u'created_by'
Trackable
to be its own table, related toLike
by a foreign key? If not, useabstract=True
and yourunique_together
will work as expected. If so, you won't be able to enforce that constraint withunique_together
. – Altdorferabstract=False
inheritance. In which case you're talking about two different tables, making it impossible to useunique_together
that way. Note thatabstract=True
will provide better performance and allow the unique constraint, so think about whether you really need to use multiple tables. – Altdorfer