Inverse of multiple inheriting classes in django

Here are my schematic models:

class Law(models.Model):
    ... 

class Bill(models.Model):
    ... # data for a proposed law, or change of an existing law

class PrivateBill(Bill):
    ... # data for a Bill that was initiated by a parliament member

class GovernmentBill(Bill):
    ... # data for a Bill that was initiated by the government

It is possible and possible that in the future I (or maybe someone else) will want to add more types of Bill.

Each bill should point to the Law (indicating which law this bill will change), and my question is: What is the best way to implement this?

If I add ForeignKey (Law) to Bill , I will have a relationship from each bill to the law, but the law will only have an inverse relationship . Accounts (bill_set), and not another inverse relationship to each type of account. Of course, I can filter out each type of account to get only those that indicate a specific Law, but this is what I think I will need to use often, so I think privatebill_set, governmentbill_set, etc. more readable.

Another possible solution is to add a foreign key to each of the inherited classes (this will give me privatebill_set, governmentbill_set, futurebill_set), but this seems hairy because I will rely on future programmers to remember to add this relationship.

?

+5
2

, Bill :

class Bill(models.Model):
    law = models.ForeignKey(Law, related_name="%(app_label)s_%(class)s_related")

    class Meta:
        abstract = True

class PrivateBill(Bill):
    pass

class GovernmentBill(Bill):
    pass
+2

- , ForeignKey, ,

+1

All Articles