多对多和多对一关系

发布于 2024-10-20 21:18:32 字数 797 浏览 3 评论 0原文

如果有人可以验证我的代码中的多对多和多对一关系,我将非常感激:

项目:

1-有不同的“类别”(例如:“世界新闻” ''欧洲''商业''体育');这些“类别”将是预定义的,并且应该更改一次(添加新/删除条目)

2-“新闻”与一个“类别”相关联。

3-“用户”可以注册任意多个“类别”(目标是开发一个显示属于“用户”注册的“类别”的“新闻”的网站。4-

用户可以选择从他的“新闻提要”中删除新闻。当发生这种情况时,“丢弃”布尔字段将设置为 True(请参阅“新闻”类

class Category(models.Model):
    category = models.CharField(max_length=50)

class News(models.Model):
    question = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    discarded = models.BooleanField()
    category = models.ForeignKey(Category)

class User(models.Model):
    name = models.CharField(max_length=50)
    subscribed_category = models.ManyToManyField(Category)

) 可以验证代码中是否正确实现了foreignKey和manytomany字段,任何改进都将受到欢迎:)

I would really appreciate it if someone can validate the many-to-many and Many-to-one relations in my code:

The project:

1- There are different "category" (ex:'world news' 'europe' 'business' 'sports'); these "category" will be predefined and should change a lot once (add new / delete an entry)

2- "news" are associated to one "category".

3- "User" can register to as many "category" as he want (the goal being to develop a site that displays "news" that falls in the "category" a "user" register to.

4- A User have the option to remove a news from his "news feed". When this happens, the "discarded" boolean field is set to True (see the "news" class).

class Category(models.Model):
    category = models.CharField(max_length=50)

class News(models.Model):
    question = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    discarded = models.BooleanField()
    category = models.ForeignKey(Category)

class User(models.Model):
    name = models.CharField(max_length=50)
    subscribed_category = models.ManyToManyField(Category)

Question:

I would really appreciate it if you can verify that the foreignKey and manytomany fields are correctly implemented in the code. Any Improvement will be more than welcome :)

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

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

发布评论

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

评论(2

独木成林 2024-10-27 21:18:33

不,这不太正确。问题出在 discarded 属性上。如果我理解正确的话,News 对象是特定类别中的项目,因此它与特定用户没有关系。如果您在新闻对象上设置discarded,则将为所有用户设置。

然而,如何对此进行建模并不完全明显,因为用户和新闻之间(正确地)没有关系。您将需要在这两个表之间添加一个类似 ManyToMany 的东西来保存丢弃的项目:

class User(models.Model):
    ...
    discarded_items = models.ManyToManyField('News')

因此,要获取每个用户的当前新闻列表,您只需检查该项目是否不在 user.discarded_items

No, this isn't quite right. The problem is with the discarded attribute. If I understand correctly, a News object is an item in a specific Category, so it has no relation with a particular user. If you set discarded on the News object, it will be set for all users.

However it's not entirely obvious how to model this, as there is (correctly) no relationship between User and News. You will need something like an additional ManyToMany between those two tables, to hold the discarded items:

class User(models.Model):
    ...
    discarded_items = models.ManyToManyField('News')

So to get the current list of news for each user, you would just check that the item is not in user.discarded_items.

药祭#氼 2024-10-27 21:18:33

你写的完全没问题并且会起作用。但是,执行此类操作的最佳方法是定义我们的模型,例如(在您的情况下):

class UsersInCategories(models.Model):
    user = models.ForeignKey(User)
    category = models.ForeignKey(Category)

原因是您可能希望稍后添加一些附加数据,例如订阅的日期时间或其他数据。请注意,当您使用 ManyToManyField 时,django 会在幕后执行此操作(一些附加表应该以这种结构出现在您的数据库中,尽管我还没有测试它)。因此,如果您不需要额外的数据,那就没问题。

What you wrote is perfectly fine and it will work. However, the best way to do such things is to define we model, for example (in your case):

class UsersInCategories(models.Model):
    user = models.ForeignKey(User)
    category = models.ForeignKey(Category)

The reason is because you may want to add some additional data later, like for example the datetime of subscribing or something else. Note that when you use ManyToManyField, then django does this behind the scene (some additional table should appear in your database with such structure, although i didn't test it yet). So if you don't need additional data, then it's ok.

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