多个模型 - 一个通用主键(Django)

时间:2021-10-25 22:08:04

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.

祝大家度过愉快的一天。正如我在标题中提到的,我想加入多个模型并使用一个全局ID识别其中一个模型,因此ID将确定它是哪个模型,我将访问其所有字段。

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.

应该返回Child1和Child2对象及其字段(fieldX,fieldY)。

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

(假设pk = 1的父记录是Child1模型)

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'

django返回一个AttributeError:'Parent'对象没有属性'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!

我的目标是为所有子模型创建一个主键。 Django有可能吗?分别是否有类似的解决方案或建议?我一直在搜索相关的主题,如contenttype或GUID,UUID,但我想这不是我正在寻找的。谢谢您的帮助!

1 个解决方案

#1


3  

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.

当您创建Parent.objects.get(pk = 1)时,您将获得Child1和Child2行,但是django ORM将返回Parent实例,因为它只检索存储在父表上的数据。

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

要获取子模型的实例,您必须执行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).

如果您有多个子类,这可能会变得非常麻烦,因为您必须确切知道父实例属于哪个子模型,才能访问它(例如,如果您尝试访问parent.child1是一个Child2实例,它会引发异常)。

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.

我建议你使用django-model-utils app,它定义了一个名为InheritanceManager的自定义管理器,你可以在你的模型上使用它,它将负责检索和返回相应的子模型实例,使整个过程变得更加容易。

#1


3  

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.

当您创建Parent.objects.get(pk = 1)时,您将获得Child1和Child2行,但是django ORM将返回Parent实例,因为它只检索存储在父表上的数据。

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

要获取子模型的实例,您必须执行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).

如果您有多个子类,这可能会变得非常麻烦,因为您必须确切知道父实例属于哪个子模型,才能访问它(例如,如果您尝试访问parent.child1是一个Child2实例,它会引发异常)。

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.

我建议你使用django-model-utils app,它定义了一个名为InheritanceManager的自定义管理器,你可以在你的模型上使用它,它将负责检索和返回相应的子模型实例,使整个过程变得更加容易。