> Profile.objects.create(name=name, foo=foo)

How do you get this `name` and `foo` variables in `save` method?


On Thu, Nov 6, 2008 at 08:20, meppum <[EMAIL PROTECTED]> wrote:

>
> I've run into a funny case and I'm not sure how to best deal with it.
> Basically, I have the following classes.
>
> class Profile(models.Model):
>    name = models.CharField()
>    foo = models.PositiveIntegerField(blank=True, default=0)
>
> class RegistrationForm(forms.Form):
>    name = forms.CharField()
>    foo = forms.IntegerField(min_value=0, max_value=55,
> required=False)
>
>   def save(self):
>       Profile.objects.create(name=name, foo=foo)
>
> If I pass in the following dictionary to RegistrationForm and save it
> I get a constraint error that foo cannot be NULL.
>
> {
>    'name': 'test',
> }
>
> I came up with two work arounds: Setting foo to 0 in the clean method
> if it is None, or changing the save method in the RegistrationForm to:
>
> def save(self):
>   profile = Profile.objects.create(name=name)
>   if foo:
>       profile.foo = foo
>       profile.save()
>
> I was wondering if there is a way to handle this case without either
> of these workarounds? It's a long shot, but I figured maybe there was
> a more elegant way that I wasn't aware of. Thanks.
>
> >
>

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

Reply via email to