Django:将多个值分配给模型字段

发布于 2025-01-31 11:05:14 字数 534 浏览 1 评论 0 原文

我正在尝试在Django模型中发挥“友好列表”功能,并且遇到了一些问题。在我的应用中,每个用户都可以制作名卡。我想让用户互相添加为“朋友”(就像fb一样),以便我可以为他们制作友好列表。

这就是我的模型。

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


# Create your models here.

class Card(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="cards")
    # friend_list = models.????

我想添加一个“ friend_list”属性,该属性可以存储其他卡的信息(我正在考虑他们的PK值)。稍后,我想迭代这些值,以便我可以在模板中使用它来制作友好列表。

例如,“乔治的名卡”应具有其朋友卡的PK值的信息。

有没有办法将多个值保存在一个属性中?

I'm trying to make a 'friendlist' function in django models and I'm having some problems. In my app, every user can make a name card. I want to let the users add each others as 'friends'(just like fb), so that I can make a friendlist for them.

This is what my models.py looks like so far.

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


# Create your models here.

class Card(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="cards")
    # friend_list = models.????

I'd like to add a 'friend_list' attribute, which can store other cards' informations(I'm thinking of their pk values). Later, I'd like to iterate those values so that I can use it to make a friendlist in the template.

For example, "George's name card" should have information of its friends' cards' pk value.

Is there a way to save multiple values in one attribute?

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

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

发布评论

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

评论(1

套路撩心 2025-02-07 11:05:14

您可以使用   [django-doc] 与与单个 card> card 相关的 user s的集合合作和与单个用户相关的多个 card 。因此,您可以将其重写为:

from django.conf import settings
from django.db import models

class Card(models.Model):
    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='owned_cards'
    )
    friends = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='befriended_cards'
    )
    # …

注意:通常最好使用 settings.auth_user_model   [django-doc] 参考用户模型,比使用用户 模型  [django-doc] 直接。有关更多信息,您可以看到 引用用户模型文档的部分

You can use a ManyToManyField [Django-doc] to work with a collection of Users that relate to a single Card and multiple Cards that relate to a single User. You can thus rewrite this to:

from django.conf import settings
from django.db import models

class Card(models.Model):
    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='owned_cards'
    )
    friends = models.ManyToManyField(
        settings.AUTH_USER_MODEL,
        related_name='befriended_cards'
    )
    # …

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

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