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



unique_together question

2008-12-09 Thread Alex Jonsson

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 ;)

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?

Thanks,
Alex
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Unique_together question

2007-03-11 Thread Rob J Goedman
Earlier I noticed the problem with edit_inline which is now fixed in  
svn.
By the way, this was the very 1st time in 5 months a daily sync with  
svn caused
an issue. Hats off to the developers!

As I mentioned, I am trying to make 'day' and 'sch' unique_together in
class ScheduleDayOfTheWeek.

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.

Thanks,
Rob

On Mar 9, 2007, at 2:14 PM, Rob J Goedman wrote:
> Hi,
>
> If I upgrade to revision 4693 (or higher) I get below error.  
> Revision 4692 works fine.
>
> Any hints? I did start to play with unique_together earlier today  
> when I noticed it,
> but don't think that's the reason. It's currently (and has been for  
> several months)
> commented out.
>
> Another small issue, if I try to make an IP address field and port  
> number 'unique_together'
> Django creates an exception with a hint to insert a typecast. It's  
> not clear to me where to
> do that. My workaround is simply to use CharField for the IP address.
>
> Thanks,
> Rob
>
> -- 
> --
>
> The models are:
>
> class Schedule(models.Model):
>   """The Schedule class."""
>   
>   name = models.CharField(maxlength=40, unique=True)
>   sch_excs = models.ManyToManyField(ScheduleHolidayException,  
> null=True, blank=True,
>   filter_interface=models.HORIZONTAL)
>   
>   def __str__(self):
>   return self.name
>   
>   def employees(self):
>   return 'Employees' % 
> self.id
>   employees.allow_tags = True
>   
>   class Admin:
>   fields = (
>   ('Name', {'fields': ('name',), }),
>   ('Holiday exceptions', {'fields': 
> ('sch_excs',), 'classes' :  
> 'collapse'}),
>   )
>   list_display = ('__str__', 'employees')
>   save_on_top = True
>   
>   class Meta:
>   db_table = 'sch'
>
>
> class ScheduleDayOfTheWeek(models.Model):
>   """Access times at days of the weeks
>   """
>   
>   day_choices = (
>   ('0', 'Sunday'),
>   ('1', 'Monday'),
>   ('2', 'Tuesday'),
>   ('3', 'Wednesday'),
>   ('4', 'Thursday'),
>   ('5', 'Friday'),
>   ('6', 'Saturday')
>   )
>   
>   day = models.CharField(maxlength=1, choices=day_choices)
>   sch = models.ForeignKey(Schedule, edit_inline=models.TABULAR,
>   num_in_admin=7, min_num_in_admin=7, max_num_in_admin=7)
>   start_time = models.TimeField(core=True, default = datetime.time 
> (0, 0, 0))
>   end_time = models.TimeField(core=True, default = datetime.time(0,  
> 0, 0))
>   
>   def __str__(self):
>   return self.day + ' of schedule ' + self.sch.name
>   
>   class Meta:
>   unique_together = (('day', 'sch'),)
>   ordering = ('day',)
>   db_table = 'sch_dow'
>   verbose_name_plural = 'Days of the week in schedules'
>

AttributeError at /admin/Application_Settings/schedule/add/
'AddManipulator' object has no attribute 'isUniqueday_sch'

Request Method:
GET
Request URL:
http://localhost:8000/admin/Application_Settings/schedule/add/
Exception Type:
AttributeError
Exception Value:
'AddManipulator' object has no attribute 'isUniqueday_sch'
Exception Location:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/db/models/fields/__init__.py in  
get_manipulator_fields, line 239
Traceback (innermost last)

Switch to copy-and-paste view

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/core/handlers/base.py in get_response
 response = callback(request, *callback_args,  
**callback_kwargs)
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/contrib/admin/views/decorators.py in _checklogin
 return view_func(request, *args, **kwargs)
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/views/decorators/cache.py in _wrapped_view_func
 response = view_func(request, *args, **kwargs)
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/contrib/admin/views/main.py in add_stage
 manipulator = model.AddManipulator()
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/db/models/manipulators.py in __init__
 self.fields.extend(f.get_manipulator_fields 
(self.opts, self, self.change, fol))
...
▶ Local vars
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site- 
packages/django/db/models/related.py in