简体   繁体   English

如何在接收模型的末端明确显示Django ManyToMany关系

[英]How to make Django ManyToMany relationships explicit on the receiving model's end

Relationships, particularly ManyToMany , in Django have always bothered me somewhat. 关系,尤其是Django中的ManyToMany关系,总是让我有些困扰。 In particular, since the relationship is only defined in one of the models, you can't tell from looking at the paired model what other relationships it might be hiding. 特别是,由于该关系仅在其中一个模型中定义,因此无法通过查看配对模型来判断它可能隐藏的其他关系。

For example, from the Django Documentation : 例如,来自Django文档

class Topping(models.Model):
    # ...

class Pizza(models.Model):
    # ...
    toppings = models.ManyToManyField(Topping)

You can tell from looking at the code that I'd find out the relevant toppings for a pizza at Pizza.toppings . 从代码中可以看出,我在Pizza.toppings找到了披萨的相关配料。 But you cannot tell that I would be able to tell what pizzas have a topping at Topping.Pizza_set --you have to look at the Pizza class to see this. 但是您不能说我能够在Topping.Pizza_set上分辨出哪些披萨是一流的- Topping.Pizza_set必须查看Pizza类才能看到这一点。

As a result, by looking at Toppings , I don't actually know the full range of fields that it has. 结果,通过查看Toppings ,我实际上并不知道它具有的所有字段。

Is there any way around this or to make it more explicit? 有什么办法可以解决这个问题,或者使其更明确? Or is there something that I'm missing? 还是我想念的东西?

This seems to be an unavoidable side effect of the DRY principle. 这似乎是DRY原理不可避免的副作用。 I don't know of any way to declaratively show the symmetry in these relations (other than by commenting and such). 我不知道有什么方法可以声明性地显示这些关系中的对称性(除了通过注释等)。 If you really want to make things explicit you could put the relationship in its own table (which Django is doing behind the scenes anyway), like: 如果您确实想使事情变得明确,则可以将关系放在自己的表中(无论如何Django都是在后台进行的),例如:

class Topping(models.Model):
    # ...

class Pizza(models.Model):
    # ...

class PizzaToppings(models.Model):
    # '+' disables the reverse relationship
    pizza = models.ForeignKey(Pizza, related_name='+') 
    topping = models.ForeignKey(Topping, related_name='+')

... but of course then you'd lose some of the convenience of the ORM. ...但是,当然,您会失去ORM的一些便利。

Found a way on Django's forum (lost link, sorry) 在Django的论坛上找到了一种方法(丢失链接,抱歉)

class Topping(models.Model):
    explicit_pizza_set = models.ManyToManyField(Pizza, through=Pizza.toppings.through, blank=True)

class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Django 的多对多关系中分组? - How to group by in ManyToMany relationships in Django? Django prefetch_related从具有多个ManyToMany关系的模型 - Django prefetch_related From Model With Multiple ManyToMany Relationships Django:外部 API 响应被映射并存储到具有多对多关系的复杂模型中 - Django: External API response to be mapped and stored into complex model with manytomany relationships Django:计算模型的ManyToMany中使用的用户 - Django: Count users used in model's ManyToMany 如何在双连接关系之后在Django中执行查询(或者:如何绕过Django对ManyToMany“模型”的限制?) - How to perform queries in Django following double-join relationships (or: How to get around Django's restrictions on ManyToMany “through” models?) Django DRF 嵌套关系 - 如何在 ManyToMany 字段中添加对象 - Django DRF nested relationships - how to add objects in a ManyToMany field Django - 如何在 ManyToMany 关系中使用 delete() 来只删除一个关系 - Django - How to use delete() in ManyToMany relationships to only delete a single relationship 如何检查 django 模型属性是否属于 ManyToMany 类型? - How to check if a django model attribute is of type ManyToMany? 如何连接 Django Model 与多对多关系? - How to Connect a Django Model with ManyToMany Relationship? manytomany model的django模板信息如何显示 - How to display in the django template information of a manytomany model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM