Inheritance for foreign keys
See original GitHub issueHi,
I cannot find a way to use the wonderful (it saves my life) InheritanceManager through a ForeignKey. Here’s some code, it will be clearer:
class Product(Model):
objects = InheritanceManager()
class Shirt(Product):
…
class Jacket(Product):
…
class Box(Model):
product = ForeignKey('Product')
boxes = Box.objects.all()
for box in boxes:
box.product # How to get shirts and jackets here?
Is there any way to do this? Cannot find it in the doc. If not, that would be a great feature to add.
Regards, Thibault
Issue Analytics
- State:
- Created 11 years ago
- Comments:20 (1 by maintainers)
Top Results From Across the Web
Foreign keys + table inheritance in PostgreSQL?
A foreign key can point to a table that is part of an inheritance hierarchy, but it'll only find rows in that table...
Read more >Inheritance and foreign keys in Postgres - DBA Stack Exchange
The table Relationship has two foreign keys set on columns Partner1 and Partner2 which reference the table Person (column ID). The (inherited) ...
Read more >Table Inheritance in Forge of Empires - InnoGames Techblog
The biggest limitations of the Table Inheritance are the unique and foreign key constraints. Such constraints are neither inherited nor ...
Read more >Inheritance and foreign keys - PostgreSQL
Hi,. I designed three tables so that one table inherits another, and the third table references the parent table.
Read more >PostgreSQL - Table Inheritance - Programster's Blog
Foreign keys don't work as expected. E.g. if you Have tables Parent, Child, and Foo, with Child inheriting from Parent, and Foo having...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@rrauenza, thanks for your comment.
I created a simple ForeignKey field which allows my models to return subclassed foreign key values.
update: It was tested only with Django-2.1/Python-3.6
I found a way to do it, so maybe I can help a little bit. First, you need to create a custom manager that always look for the subclasses:
After that, you plug it into your model and set it as the base manager: