question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inheritance for foreign keys

See original GitHub issue

Hi,

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:open
  • Created 11 years ago
  • Comments:20 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
renatgalimovcommented, May 8, 2018

@rrauenza, thanks for your comment.

I created a simple ForeignKey field which allows my models to return subclassed foreign key values.

class InheritanceForwardManyToOneDescriptor(ForwardManyToOneDescriptor):
    def get_queryset(self, **hints):
        return self.field.remote_field.model.objects.db_manager(hints=hints).select_subclasses()


class InheritanceForeignKey(models.ForeignKey):
    forward_related_accessor_class = InheritanceForwardManyToOneDescriptor

update: It was tested only with Django-2.1/Python-3.6

4reactions
itsmealvescommented, Jan 9, 2019

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:

from model_utils.managers import InheritanceManager

class ProductManager(InheritanceManager):
	def get_queryset(self):
		return super(ProductManager, self).get_queryset().select_subclasses()

After that, you plug it into your model and set it as the base manager:

class Product(Model):
	objects = ProductManager()

	class Meta:
		base_manager_name = 'objects'
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found