Re: Inverted checkbox value in modelForm field for BooleanField?

2011-05-14 Thread Andy McKay

On 2011-05-14, at 6:08 PM, br wrote:
> However, come interface tweaking time (now), we decided we want to
> display a page with a check box "Auto Cycle?" instead of "click-
> through", so I basically need to invert the actual value with respect
> to this interface .
> What is the easiest way to implement this?

You can access the field and initial data inside the form init and do anything 
you want there. Which depends upon how you are calling your Model form. If you 
are passing through an instance, for example, you could tweak it:

class MyForm(ModelForm):
  def __init__(self, *args, **kw):
kw['instance'].click_through = True
super(MyForm, self).__init__()

--
  Andy McKay
  a...@clearwind.ca
  twitter: @andymckay
 

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Inverted checkbox value in modelForm field for BooleanField?

2011-05-14 Thread br
Conceptually, an interface element of my application can either be
click through (i.e. requires user interaction) or auto-cycle (no user
interaction).  So its boolean.  But i picked the wrong one when I
originally designed the model and now we have existing data and I
don't want to change it.

So I have a database field

class MyModel(Model):
 click_through = models.BooleanField(
 . . . )

And a modelform:

class MyForm(ModelForm):

class Meta:
model = MyModel
fields = ('name', 'click_through')

However, come interface tweaking time (now), we decided we want to
display a page with a check box "Auto Cycle?" instead of "click-
through", so I basically need to invert the actual value with respect
to this interface .

What is the easiest way to implement this?

I realize i could create a custom Field class and override to_python
to invert the value after the Form is submitted, but how about
inverting the value after i initiall load an instance of the value and
before it goes into the form, so that it displays a checked box if
click-through is False and vice-versa?  Is there an override similar
to to_python for this, or is there another way of going about it?


Thanks,

Ben

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.