views:

202

answers:

1

Hello 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 ?

+1  A: 

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

uswaretech
thanks but seems like unique_together is not supported for Manytomany fields :Smain.channelstatus: "unique_together" refers to platform. ManyToManyFields are not supported in unique_together:main.channelstatus: "unique_together" refers to platform. This is not in the same model as the unique_together statement.
Added a new answer does that help you?
uswaretech