django 每个日期都是唯一的

发布于 2024-08-02 16:51:52 字数 359 浏览 3 评论 0原文

首先,让我向您展示我的模型:

class ChannelStatus(models.Model):

        channel = models.ForeignKey(Channel,unique_for_date="date")

        date = models.DateField()

        platform = models.ManyToManyField(Platform)

现在,当我想要创建一个 ChannelStatus 条目时,不能有多个条目具有相同的频道和日期。现在,我想将其更改为每个条目的唯一性渠道到每个平台的日期,因此不同平台可以有多个具有相同日期的相同渠道。我怎样才能做到这一点?

First of all let me show you my model:

class ChannelStatus(models.Model):

        channel = models.ForeignKey(Channel,unique_for_date="date")

        date = models.DateField()

        platform = models.ManyToManyField(Platform)

Right now, when I want to make an ChannelStatus entry, there cant be more than one entry which is the same channel and the date.Now, I want to change this as the uniquess of each channel to the date for each platform, so different platforms can have several same channel with same date. How can I achieve this ?

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

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

发布评论

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

评论(1

堇年纸鸢 2024-08-09 16:51:52

好吧,那这个呢。 (未经测试的代码。)

class ChannelStatus(models.Model):

        channel = models.ForeignKey(Channel)

        date = models.DateField()
        class Meta:
            unique_together = ('channel', 'date')



class ChannelM2M(models.Model):
    channel_status = models.ForeignKey(Channel)
    platform = models.ForeignKey(Platform, unique = True)

[旧答案]

 class Meta:
    unique_together = ('channel', 'date', 'platform')

顺便说一句,我会更改日期中第二个字段的名称,您正在使用日期时间,有时会执行 from datetime import date ,您将得到被咬

Ok, what about this. (Untested code.)

class ChannelStatus(models.Model):

        channel = models.ForeignKey(Channel)

        date = models.DateField()
        class Meta:
            unique_together = ('channel', 'date')



class ChannelM2M(models.Model):
    channel_status = models.ForeignKey(Channel)
    platform = models.ForeignKey(Platform, unique = True)

[Old answer]

 class Meta:
    unique_together = ('channel', 'date', 'platform')

Btw, I would change the name of the second field from date, one you are working with datetime, and sometime do from datetime import date, you are going to get bitten

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