On 9 déc, 13:20, Alex Jonsson <[EMAIL PROTECTED]> wrote:
> Hey everyone,
>
> My model looks like the following:
>
> http://dpaste.com/97293/
>
> An album has a release date for every format, and one format can only
> have one release date per album. Hopefully you get what I'm trying to
> achieve here ;)
Then your model seems broken to me. If you set album as a unique key,
you can only have one single ReleaseDate instance per album. Idem for
format: if a unique key, you can only have one single ReleaseDate
instance per format. You want them to be unique _together_, not unique
in the whole table.
> The thing is though, that in my Album model I specify a ManyToMany
> relationship with the Format in order to specify what Formats an Album
> would be released in.
>
> Now when I create this model, it will allow me to set a release date
> for any Format - I would like to allow it to only specify a release
> date for the formats that are specified in the Album ManyToMany
> column.
>
> How would be the easiest way to do this?
overload the ReleaseDate save() method ?
class ReleaseDate(models.Model):
album = models.ForeignKey(Album) #, unique=True)
format = models.ForeignKey(Format) #, unique=True)
date = models.DateField()
class Meta:
verbose_name = _('release date')
verbose_name_plural = _('release dates')
unique_together = ('album', 'format')
def save(self, **kw):
if self.format not in album.format_set.all():
raise Whatever(
"%s is not an allowed format for alubm %s" \
% (self.format, self.album)
)
super(ReleaseDate, self).save(**kw)
Note that this might cause problem if you:
- allow a format for an album
- create a ReleaseDate for this album and format
- disallow the format for this album
You'll have to handle this correctly (most probably by overloading
Album.save too...)
HTH
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---