管理者、模型继承或者 django 中的用户切片是什么?

发布于 2024-09-03 12:29:22 字数 962 浏览 4 评论 0原文

我正在 Django 中编写一个项目,其中有 5 种用户组:

  • Group1
  • Group2
  • ...

然后我有一个模型,Item,它与用户有很多关系,该 Item 有一个所有者(Group1 中的用户)、客户(Group2 中的用户)和许多相关用户(Group3 中的用户)。

我想知道哪种是书写这种关系的正确方法。我很想写一些类似的东西:

class Item(models.Model):
    owner = models.ForeignKey(Owner)
    customer = models.ForeignKey(Customer)
    users = models.ManyToManyField(RelatedUser)

以某种方式定义了 Owner、Customer 和latedUser 类。

我不知道如何实现这一点。我不想使用模型继承,因为我只想要一个表 User。即使经理似乎也没有帮助我。实际上我正在使用这样的东西:

try:
    customer = models.ForeignKey(User,
                              related_name='cust',
                              limit_choices_to = {'groups__in': [Group.objects.get(name = 'customers')]})
except:
    customer = models.ForeignKey(User,
                              related_name='cust')

主要是因为当启动一个空数据库组“客户”时不存在并且会引发错误。

哪种方式才是负担得起的正确方式?

提前致谢

I'm writing a Project in Django where I've 5 kind of groups of Users:

  • Group1
  • Group2
  • ...

Then I've a Model, Item which has many relation with users, the Item has one Owner (a User in Group1), a Customer (an User in Group2) and many RelatedUser (Users in Group3).

I'm wondering which is the correct way to write this relations. I'd love to write something like:

class Item(models.Model):
    owner = models.ForeignKey(Owner)
    customer = models.ForeignKey(Customer)
    users = models.ManyToManyField(RelatedUser)

Having defined in some way Owner, Customer and RelatedUser classes.

I do not know how to achieve this. I do not want to use model inheritance, because I just want a table User. Even Managers does not seems to help me. Actually I'm using something like this:

try:
    customer = models.ForeignKey(User,
                              related_name='cust',
                              limit_choices_to = {'groups__in': [Group.objects.get(name = 'customers')]})
except:
    customer = models.ForeignKey(User,
                              related_name='cust')

Mostly because when starting form an empty database Group 'customers' does not exists and errors are raised.

Which is the right way to afford this?

Thanks in advance

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

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

发布评论

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

评论(1

小草泠泠 2024-09-10 12:29:22

您可以为每种用户类型定义单独的模型 - 每个模型都有一个 User 的 ForiegnKey。优点是简单,但缺点是这种方法添加了多个表,并且如果您稍后需要添加更多组,则不是特别可扩展。

另一种选择是定义一个组模型,该模型存储可用的不同类型的组,并且与用户具有多对多关系(假设一个用户可以位于多个组中)。

通过为组模型创建固定装置,您可以解决从新数据库开始时未定义组的问题。夹具是一个文本文件(默认为 JSON 格式),它定义了一组可以轻松自动或手动加载到数据库中的数据。可以使用 dumpdata 管理命令。

如果您希望自动加载固定装置(当您运行syncdb时),请在应用程序中创建一个fixtures目录,并将固定装置命名为initial_data。您还可以创建其他固定装置并使用 loaddata 命令加载它们,或者在测试中通过指定 特定TestCasefixtures列表

You could define separate models for each user type - each with a ForiegnKey to User. The upside is simplicity, but the down side is that this approach adds multiple tables, and isn't particularly extensible if you need to add more groups later.

Another option is to define a Groups model, which stores the different types of groups available, and has a ManyToMany relationship to User (assuming one user can be in multiple groups).

You can get around the problem of no groups being defined when starting from a new database by creating a fixture for the Groups model . A fixture is a text file (default is JSON format) that defines a set of data that can be easily loaded into the DB, either automatically or manually. Fixtures can be easily created from existing data with the dumpdata management command.

If you wish a fixture to be loaded automatically (when you run syncdb), create a fixtures directory in your app, and name the fixture initial_data. You can also create other fixtures and load them with either the loaddata command, or in your tests by specifying a fixtures list for a particular TestCase

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