我应该在哪种模型中添加ManyToManyField? [英] In what model should I add the ManyToManyField?

查看:88
本文介绍了我应该在哪种模型中添加ManyToManyField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关多对多关系的文档示例。我找不到关于 ManyToManyField 放置位置的提示。就我而言,我有一个扩展的用户模型 Client 和一个模型 Pizza 。每个客户都可以将一个或多个比萨饼标记为收藏夹。那是我的两个模型:

I read the documentation about many-to-many relationships and the examples. What I could not find is a hint on where to put the ManyToManyField. In my case I have an extended user model Client and a model Pizza. Every client may mark one or more pizzas as favourites. Those are my two models:

from django.db import models
from django.contrib.auth.models import User

class Client(models.Model):
    user = models.OneToOneField(User)
    #? favourite_pizza = models.ManyToManyField()

class Pizza(models.Model):
    name = models.CharField(max_length=255)
    #? favourite_pizza = models.ManyToManyField()

我应在哪种模型中添加ManyToManyField? strong>这有关系吗?

In what model should I add the ManyToManyField? Does it matter?

PS重要信息是客户有多少个喜欢的披萨(以及哪个)。有多少客户将比萨饼标记为收藏夹(以及谁),这一点并不重要。因此,我选择将 ManyToManyField 放在 Client 类中。

PS The important information is how many favourite pizzas a client has (and which). It is less important how many clients marked a pizza as a favourite (and who). Consequently I would chose to put the ManyToManyField in the Client class.

推荐答案

来自Django 文档

From the Django documentation:


通常,ManyToManyField实例应该放在要在表单上进行编辑的对象中。

Generally, ManyToManyField instances should go in the object that’s going to be edited on a form.

这篇关于我应该在哪种模型中添加ManyToManyField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆