werefr0g—
Yep, that's actually the solution I'm looking into right now. However,
I'm getting an error when trying to save a new instance of the
Restaurant model:
"'Restaurant' instance needs to have a primary key value before a many-
to-many relationship can be used."
Here's the new code that's triggering this error:
from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
class Restaurant( models.Model ):
user = models.ForeignKey( User )
name = models.CharField( max_length = 128 )
slug = models.CharField( max_length = 24, unique = True )
cuisines = models.ManyToManyField( 'Cuisine', help_text = 'Choose
up to three' )
def __unicode__(self):
return self.name
def clean( self ):
raise ValidationError( type( self.cuisines ).__name__ )
class Cuisine( models.Model ):
name = models.CharField( max_length = 32 )
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
As you can see, my custom clean() method simply raises an error
containing the type of the cuisines property. And even this is enough
to trigger the error shown above. Doing len( self.cuisines ) gives the
same result. (Raising an error containing a literal string, without
attempting to inspect the cuisines property, works as expected.)
Any ideas? It really seems like it should be possible to perform this
check at the model level. (bagheera, I'll resort to form-level
validation if I have to—but only if I have to. Client-side checks are
for UI convenience only, which is a peripheral issue.)
I'm new to Django and Python, and at this point I'm more interested in
learning the Right Way to do this (if there is one), or *why* I can't
do it at the model level (if, in fact, I can't).
On Mar 11, 4:11 pm, gontran <[email protected]> wrote:
> I didn't try it, but werefr0g may be right. It seems that
> Model.clean() is the method do you need. And you can still add a
> javascript control for more convenience without the risk that if a
> user deactivate javascript, the error will be saved.
>
> Let us know greenie if it's ok with this method.
>
> On 11 mar, 22:03, werefr0g <[email protected]> wrote:
>
>
>
>
>
>
>
> > Hello,
>
> > Can Model.clean() method help you? [1] You'll still have to pay
> > attention to validation before trying to save your instances.[2]
>
> > Regards,
>
> > [1]http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db....
> > [2]http://docs.djangoproject.com/en/dev/releases/1.2/#model-validation
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.