For example the following model form does not display any widgets for
full_name and full_name2:
class PersonForm(forms.ModelForm):
class Meta:
model = Person
Adding full_name and full_name2 to the model form doesn't help either as
the variables are not bound to the properties:
class PersonForm(forms.ModelForm):
full_name = forms.CharField()
full_name2 = forms.CharField()
class Meta:
model = Person
Any ideas? Or is it impossible?
Boris
Not impossible - just override the save method on that form to
manually set the model property based on the form value, then call
super to proceed with the save.
> Boris
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>
> On 18 February 2010 10:49, Boris Schaeling <bo...@highscore.de> wrote:
>> Is it possible to make a model's properties available in a form which
>> are
>> created as described at
>> http://www.djangoproject.com/documentation/models/properties/?
> [...]
> Not impossible - just override the save method on that form to
> manually set the model property based on the form value, then call
> super to proceed with the save.
This replaces the setter but not the getter. There is eg. no load method
to process a value loaded from a database?
Boris
To replace the getter, you'd have to override the modelform's __init__
method and set the corresponding field value from the instance getter.
--
Gonzalo Delgado <gonza...@gmail.com>
> El 18/02/10 10:18, Boris Schaeling escribiᅵ:
>>
>> This replaces the setter but not the getter.
>
> To replace the getter, you'd have to override the modelform's __init__
> method and set the corresponding field value from the instance getter.
I see. Then it means what I actually tried to do is impossible. I can't
use properties as described at
http://www.djangoproject.com/documentation/models/properties/ but should
overwrite save() and __init__() because for a property there is no way to
display a widget in a model form?
I would prefer to use a property because I want to pre- and post-process a
value from the database (exactly what properties are good for). Having the
original value from the database at the same time available in another
attribute (which is used by the property) would have also been nice (and
if only for debugging purposes) instead of messing with it in save() and
__init__().
Boris
> [...]I see. Then it means what I actually tried to do is impossible. I
> can't use properties as described at
> http://www.djangoproject.com/documentation/models/properties/ but should
> overwrite save() and __init__() because for a property there is no way
> to display a widget in a model form?
I tried now to come up with a solution based on Sam's and Gonzalos'
proposals. The following code is loosely based on the example from
http://www.djangoproject.com/documentation/models/properties/ (a bit
shortened to make it less complicated):
class Person(models.Model):
first_name = models.CharField(max_length=30)
@property
def user_first_name(self):
return str(self.first_name).rstrip('X')
class PersonForm(forms.ModelForm):
user_first_name = forms.CharField(max_length=30)
class Meta:
model = Person
exclude = ['first_name']
def save(self, commit=True):
q = super(PersonForm, self).save(False)
q.first_name = self.cleaned_data['user_first_name'] + 'X'
if commit:
q.save()
return q
Now it's possible to save a different value in the database than the one
used elsewhere in the project. In the database each and every name ends
with X. To display the name without X the user_first_name property can be
used. To make sure that X is appended to every name in the database save()
has been overwritten.
It is important to exclude the model's attribute first_name in the form
(otherwise the name with the X is shown). In order to provide an input
field nevertheless a new unbound attribute must be added (called
user_first_name). This unbound attribute is wired with the model's
first_name attribute in save().
I think that's as simple as it can get?
Boris