[Fixed]-Django: GenericForeignKey and unique_together


Your use of the generic foreign key in this situation is correct.

The error is coming from your unique_together declaration in your model. unique_together can only be used with columns that exist in the database. Since contentObject is not a real column, Django complains about the constraint.

Instead, you can do the following:

unique_together = (('contentType', 'contentId', 'sharedWidth'),)

This is equivalent to what you had defined in your question because contentObject is really just the combination of contentType and contentId behind the scenes.

Leave a comment