我正在做一个项目,其中用户可以添加一个事件,另一个用户可以自己注册该事件。每当用户注册活动时,我都想将其姓名添加到列表中。
我的型号:
class Event(models.Model):
Topic = CharField
participants = # want a field here
# which can store
# multiple items
# that is the name
# of the user. So when
# the user
# registers a
# method appends
# the user name in this list.
您需要一个ManyToManyField来将用户链接到Events:
class Event(models.Model):
topic = CharField()
participants = models.ManyToManyField(User)
您也可以添加一个单独的";通过";表添加有关参与的额外信息:
class Event(models.Model):
topic = models.CharField()
participants = models.ManyToManyField(User, through='Participation')
class Participation(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
event = models.ForeignKey(Event, on_delete=models.CASCADE)
date_joined = models.DateTimeField(auto_now_add=True)