user1427661
user1427661

Reputation: 11774

Foreign Key Possibility of Two Models

Let's say I have two models, Entry and Post. Simple models, but they share several fields (subject, description, etc... this is hypothetical since I'm still in the design stage). I have another field, Acknowledgement, which is simply an association between an Entry or a Post and a Customer. If it were implemented only for Entry relationships, it would look like this:

class Acknowledgement(models.Model)

    entry = models.ForeignKey(Entry)
    customer = models.ForeignKey(Customer)

However, I want it to be able to function as a join table for EITHER Entry or Post and Customer, never both. I'm vaguely familiar with generic keys as an option, but I don't know if that's the best route here, since I'd like to restrict the foreign key to one of those two model types. Any ideas on a good approach for this?

Upvotes: 0

Views: 43

Answers (1)

Ramez Ashraf
Ramez Ashraf

Reputation: 891

The route that i would take is:

Override save() method of the model Acknowledgement , and in save() check if relations are fine or if not, throw an exception.

Upvotes: 1

Related Questions