0

我正在开发一个用户可以添加事件的项目,而另一个用户可以自己注册该事件。每当他们参加活动时,我都想将用户名添加到列表中。

我的模型:

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.
4

1 回答 1

0

您需要一个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)
于 2020-09-08T13:47:11.360 回答