Re: overwrite the save method

2010-08-11 Thread Nick
You override the save in two places, in the form itself or in the
view. Either one would work, you could combine the two as well.

the form save override would look something like

def save(self):
formatform = super(FormFormat, self).save(commit=False)
if formatform.boolean1 and formatform.boolean2:
formatform.myBoolean = True
formformat.save()

in the view:

form = FormFormat(request.POST)
if form.is_valid():
form_save = form.save(commit=False)
if form_save.boolean1 == True and form_save.boolean2 == True:
form_save.myBoolean = True
form_save.save()
On Aug 11, 2:08 pm, Roald de Vries  wrote:
> On Aug 11, 2010, at 7:25 PM, refreegrata wrote:
>
>
>
> > My code
> > --
> > class Format(models.Model):
> >    name = models.CharField(max_length=5, unique=True)
> >    myBoolean = models.BooleanField(default=False)
>
> > class FormFormat(forms.ModelForm):
> >    boolean1 = forms.BooleanField(required=False)
> >    boolean2 = forms.BooleanField(required=False)
>
> >    class Meta:
> >        model = Format
> >        fields = ['name']
>
> > FormsetFormFormat = forms.models.modelformset_factory(Format,
> > max_num=0,form=FormFormat)
> > --
> > The idea is this:
>
> > if boolean1=True and boolean2=True the field myBoolean must to be True
> > if boolean1=True and boolean2=False the field myBoolean must to be
> > False
> > ...
> > My question is, how i can do this?
> > overwriting the save method?
>
> Possible, but I have a feeling there might be a nicer solution. What  
> do you want to happen if boolean1 is false?
>
> > I don't have idea, because boolean1 and boolean2 are form fields not
> > model fields. Can i pass custom parameters to the save method?
>
> Yes, you can.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: overwrite the save method

2010-08-11 Thread Roald de Vries

On Aug 11, 2010, at 7:25 PM, refreegrata wrote:

My code
--
class Format(models.Model):
   name = models.CharField(max_length=5, unique=True)
   myBoolean = models.BooleanField(default=False)

class FormFormat(forms.ModelForm):
   boolean1 = forms.BooleanField(required=False)
   boolean2 = forms.BooleanField(required=False)

   class Meta:
   model = Format
   fields = ['name']

FormsetFormFormat = forms.models.modelformset_factory(Format,
max_num=0,form=FormFormat)
--
The idea is this:

if boolean1=True and boolean2=True the field myBoolean must to be True
if boolean1=True and boolean2=False the field myBoolean must to be
False
...
My question is, how i can do this?
overwriting the save method?


Possible, but I have a feeling there might be a nicer solution. What  
do you want to happen if boolean1 is false?



I don't have idea, because boolean1 and boolean2 are form fields not
model fields. Can i pass custom parameters to the save method?


Yes, you can.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: overwrite the save method

2010-08-11 Thread bagheera

--
class Format(models.Model):
name = models.CharField(max_length=5, unique=True)
myBoolean = models.BooleanField(default=False)

class FormFormat(forms.ModelForm):
boolean1 = forms.BooleanField(required=False)
boolean2 = forms.BooleanField(required=False)

class Meta:
model = Format
fields = ['name']

FormsetFormFormat = forms.models.modelformset_factory(Format,
max_num=0,form=FormFormat)
--
The idea is this:

if boolean1=True and boolean2=True the field myBoolean must to be True
if boolean1=True and boolean2=False the field myBoolean must to be
False
...
My question is, how i can do this?
overwriting the save method?

Well, i'm new one myself, but if i understand docs correctly, You can  
create form from model, then add b1 and b2 fields, and include only them  
for display by using 'fields' meta option.

Later, when form is valid, check b1 and b2, and set myBoolean

http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets


--
Linux user

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



overwrite the save method

2010-08-11 Thread refreegrata
Hello list, i have a newbie question

My code
--
class Format(models.Model):
name = models.CharField(max_length=5, unique=True)
myBoolean = models.BooleanField(default=False)

class FormFormat(forms.ModelForm):
boolean1 = forms.BooleanField(required=False)
boolean2 = forms.BooleanField(required=False)

class Meta:
model = Format
fields = ['name']

FormsetFormFormat = forms.models.modelformset_factory(Format,
max_num=0,form=FormFormat)
--
The idea is this:

if boolean1=True and boolean2=True the field myBoolean must to be True
if boolean1=True and boolean2=False the field myBoolean must to be
False
...
My question is, how i can do this?
overwriting the save method?

I don't have idea, because boolean1 and boolean2 are form fields not
model fields. Can i pass custom parameters to the save method?

Thanks for read

P.D.:sorry for my poor english

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.