姜戈 - 你能解释一下吗?

发布于 2024-12-12 19:40:06 字数 707 浏览 0 评论 0原文

我有三个模型:

  1. System_Contact
  2. System
  3. Contact_list

Contact_List 模型有两个字段: contact > 和 sys ,毫不奇怪,它只是一个将联系人列表与每个系统关联的多对多模型。我有 modelForm 用于将新联系人添加到系统的联系人列表中:

   class Add_Contact_Form(ModelForm):
       class Meta:
           model = Contact_List
           fields = ('contact',)

很简单,对吗?我的困惑是:即使认为 Contact_List 模型有许多很多重复的联系人(因为一个联系人可以与许多系统关联),每个联系人都是仅在表单的 Select 小部件中显示一次。

为什么?!

我的意思是,对于我的目的来说,这是一个很好的默认行为,但我想确保这实际上是我可以依赖的正确默认行为,而不是我所做的一些随机错误。现在就为我锻炼吧。

I have three models:

  1. System_Contact
  2. System
  3. Contact_list

The Contact_List model has two fields: contact and sys and, not surprisingly, is just a manyToMany model to associate a list of contacts to each system. I have modelForm for adding a new contact to the system's list of contacts:

   class Add_Contact_Form(ModelForm):
       class Meta:
           model = Contact_List
           fields = ('contact',)

Simple, right? My confusion is this: Even thought the Contact_List model has many many duplicate contacts (because one contact can be associated with many systems) each contact is only displayed once within the form's Select widget.

Why?!

I mean, this is a great default behaviour for my purposes, but I want to make sure this is actually the correct default behaviour that I can rely on, not some random error I have done that just happens to work out for me now.

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

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

发布评论

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

评论(1

街角卖回忆 2024-12-19 19:40:06

这并不是说它是默认行为,而是 contact_list 表单中的选择小部件正在显示联系人表中的所有条目。

每个模型都是数据库中的一个表,因此您有 3 个表:

  • ContactTable - 表中的每一行都是一个人
  • SystemTable - 表中的每一行都是一台计算机(为了争论)
  • ContactListTable - 每行都是一个映射系统和用户列表之间的关系

如果这就是您想要做的,您应该具有以下内容:

class Contact(models.Model):
    name = ...

class System(models.Model):
    type = ...

class ContactList(models.MOdel):
    system = models.ForeignKey(System)
    contacts = models.ManyToManyField(Contact)

这意味着 ContactList 表中的每一行都是系统表中的特定计算机与来自的联系人列表之间的关系联系表

It's not that it's default behaviour, it's that the select widget in your contact_list form is displaying all of the entries that are from the contact table.

Every model is a table in the database, therefore you have 3 tables:

  • ContactTable - where every row in the table is a person
  • SystemTable - where every row in the table is a computer (for arguements sake)
  • ContactListTable - where every row is a mapping between a system and a list of users

If this is what you are trying to do, you should have the following:

class Contact(models.Model):
    name = ...

class System(models.Model):
    type = ...

class ContactList(models.MOdel):
    system = models.ForeignKey(System)
    contacts = models.ManyToManyField(Contact)

This means that every row in the ContactList table is a relationship between a particular machine from the system table and a list of contacts from the contact table

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