Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple models - One common primary key (Django)

I wish a nice day to everyone. As I've mentioned in title, I would like to join multiple models and identify one of them with one global ID, so the ID would determine which model it is and I would access all of its fields.

Currently I'm fighting with multi-table inheritance and I have a problem. I would like to have one parent model and several child models which would inherit some of parent's fields.

class Parent(models.Model):
    pass

class Child1(Parent):
    fieldX = models.CharField()

class Child2(Parent):
    fieldY = models.CharField()

However I would like to access the childs by parent model using primary key. So...

Parent.objects.all()

should return Child1 and Child2 objects with their fields (fieldX, fieldY) as well.

(Let assume that Parent record with pk=1 is Child1 model)

Though when I try to access child fields by parent model

child = Parent.objects.get(pk=1)
child.fieldX

django returns an AttributeError: 'Parent' object has no attribute 'fieldX'

My goal is to create something like one primary key for all child models. Is that possible in Django? Respectively, is there any similar solution or suggestion? I have been searching in related topics like contenttype or GUID, UUID, but I suppose that's not what I'm looking for. Thank you for your help!

like image 438
optimista Avatar asked Feb 21 '13 00:02

optimista


People also ask

Can you have multiple primary keys in Django?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.

How can I have multiple models in a single Django Modelform?

You can just show both forms in the template inside of one <form> html element. Then just process the forms separately in the view. You'll still be able to use form.

How do I merge two Django models?

1 Answer. Show activity on this post. In your models Device and History models are related with a foreign key from History to DeviceModel, this mean when you have a History object you can retrieve the Device model related to it, and viceversa (if you have a Device you can get its History).

What is __ str __ In Django model?

str function in a django model returns a string that is exactly rendered as the display name of instances for that model.


1 Answers

When you make Parent.objects.get(pk=1) you are getting Child1 and Child2 rows alright, but django ORM is returning you Parent instances, since it's only retrieving the data stored on the parent table.

To get an instance of the children model, you have to do parent_instance.childmodelname.

This can get messy real quick if you have multiple child classes, since you have to know exactly to which child model the parent instance belongs to, to be able to access it (if you try, for example, to access parent.child1 being a Child2 instance, it will raise an exception).

I recommend you use the django-model-utils app, which defines a custom manager called InheritanceManager which you can use on your model and will take care of retrieving and returning the corresponding child model instance, making the whole process a lot easier.

like image 85
asermax Avatar answered Nov 02 '22 06:11

asermax