多个模型或查询集的外键

发布于 2024-10-14 08:15:25 字数 224 浏览 5 评论 0原文

是否可以为多个模型创建一个外键?我想从不同的模型中进行选择,例如零件和机器模型。

我阅读此内容将多个模型合并到一个列表中: 如何在 Django 中组合多个查询集?

如何获取该列表的外键?

Is it possible to make a ForeignKey to more than one model? I want to choose from different models like Parts and Machines Model.

I read this to combine multiple models into one list: How to combine multiple querysets in Django?

How can I get the foreign key to that list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2024-10-21 08:15:25

我知道您一年多前就问过这个问题,但我也遇到了类似的问题,我想为未来的读者分享解决方案的链接。

一般来说,contenttypes框架解决了这个问题,并且我想这就是丹尼尔·罗斯曼所说的。

如何在 Django 中使用动态外键?

I know that you asked this over a year ago, but I had a similar problem and I want to share a link to the solution for future readers.

Generally the contenttypes framework solves this problem, and I guess this is what Daniel Roseman was talking about.

How to use dynamic foreignkey in Django?

陪你到最终 2024-10-21 08:15:25

您需要通用关系

通用关系允许您动态定位外键的模型。

You need generic relations.

A generic relation allows you to dynamically target the Model of the foreign key.

宁愿没拥抱 2024-10-21 08:15:25

我将为这个问题提供一个全面的答案,我知道它已经很老了,但它仍然相关。

我们将使用 通用关系

首先,在 settings.py 中,确保 django.contrib.contenttypes 包含在 INSTALLED_APPS 数组中。

让我们在 models.py 中创建一个新模型:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

通过 content_type,我们可以将 Image 与任何其他模型类关联,而 object_id 将保存另一个模型实例。

class Image(models.Model):
    image = models.ImageField(
      upload_to="imgs/products", blank=True)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

要从 Company 实例引用 Image 模型,我们需要创建一个 反向泛型关系

class Company(models.Model):
  name = models.CharField(max_length=100)
  images = GenericRelation(Image)

schema.py 中,我们可以创建 ImageCompany 实例中,例如:

company_instance = Company(name="Apple")
company_instance.save()
for img in imgs:
    #Image(image=img, content_object=company_instance)
    company_instance.images.create(image=img)
company_instance.images.all() # fetch all images

company_instance.images 字段只是一个 GenericRelatedObjectManager (docs

这是最终的Image 表在数据库中的外观:
图像表

I'll provide a comprehensive answer for this question, I know its quite old, but it's still relevant.

We're gonna be using Generic Relations.

First, in settings.py make sure that django.contrib.contenttypes is included in the INSTALLED_APPS array.

Let's create a new model in models.py:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation

With content_type we can associate Image with any other model class, while object_id will hold the other model instance.

class Image(models.Model):
    image = models.ImageField(
      upload_to="imgs/products", blank=True)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()

To refer back to the Image model from a Company instance we need to make a reverse generic relation

class Company(models.Model):
  name = models.CharField(max_length=100)
  images = GenericRelation(Image)

In schema.py, we can create Images in a Company instance like:

company_instance = Company(name="Apple")
company_instance.save()
for img in imgs:
    #Image(image=img, content_object=company_instance)
    company_instance.images.create(image=img)
company_instance.images.all() # fetch all images

the company_instance.images field is just a GenericRelatedObjectManager (docs)

This is how the final Image table looks in the database:
image table

杯别 2024-10-21 08:15:25

Django 多态库 提供了一个简单的解决方案,应该可以很好地工作管理和表单也使用表单集。

例如:

from polymorphic.models import PolymorphicModel


class BookOwner(PolymorphicModel):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)

class StaffBookOwner(BookOwner):
    owner = models.ForeignKey(Staff, on_delete=models.CASCADE)

class StudentBookOwner(BookOwner):
    owner = models.ForeignKey(Student, on_delete=models.CASCADE)

这样,您可以使用父模型将所有者设置为 StaffStudent 实例,或者直接使用子模型。

The Django-polymorphic library provides a simple solution that should work well with the admin and forms too using formsets.

For example:

from polymorphic.models import PolymorphicModel


class BookOwner(PolymorphicModel):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)

class StaffBookOwner(BookOwner):
    owner = models.ForeignKey(Staff, on_delete=models.CASCADE)

class StudentBookOwner(BookOwner):
    owner = models.ForeignKey(Student, on_delete=models.CASCADE)

With this, you can use the parent model to set the owner to either a Staff or Student instance or use the child models directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文