Re: unique_together question

2008-12-09 Thread bruno desthuilliers

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
-~--~~~~--~~--~--~---



Re: Unique_together question

2007-03-12 Thread Rob J Goedman

Thanks Ramiro,

Thanks for the pointer and hint. Next time I will search there first.

That indeed gets it through the validator.  The error seems to be  
caught by some
lower level (db?), but for now that works.

Thanks again,
Rob

On Mar 11, 2007, at 4:45 PM, Ramiro Morales wrote:

>
> On 3/11/07, Rob J Goedman <[EMAIL PROTECTED]> wrote:
>
>>
>> Any reason I can't combine these 2 fields to get a valid  
>> 'isUniqueday_sch'
>> in the AddManipulator? Full traceback at the end of the email.
>>
>
> This is a problem already reported. See tickets #526 and #2470,  
> there is a
> workaround described in the comments that involves changing the  
> order of the
> fields  in unique_together.
>
> Regards,
>
> -- 
>  Ramiro Morales
>
> >


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Unique_together question

2007-03-11 Thread Ramiro Morales

On 3/11/07, Rob J Goedman <[EMAIL PROTECTED]> wrote:

>
> Any reason I can't combine these 2 fields to get a valid 'isUniqueday_sch'
> in the AddManipulator? Full traceback at the end of the email.
>

This is a problem already reported. See tickets #526 and #2470, there is a
workaround described in the comments that involves changing the order of the
fields  in unique_together.

Regards,

-- 
 Ramiro Morales

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---