我正在尝试在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?
发布评论
评论(1)
您可以使用   [django-doc] 与与单个
card> card
相关的user
s的集合合作和与单个用户
相关的多个card
。因此,您可以将其重写为:You can use a
ManyToManyField
[Django-doc] to work with a collection ofUser
s that relate to a singleCard
and multipleCard
s that relate to a singleUser
. You can thus rewrite this to: