What I have done is this:
@classmethod
def load_from_model(clazz, model):
data = {}
fields = clazz.base_fields
for field in fields.keys():
try:
data[field]=getattr(model,field)
except AttributeError:
print field, "is not present in", model
#add non-simple mappings here. eg:
data['esso']=model.child.esso
form = clazz(data)
return form
def save_to_model(self, model):
model.user.first_name = self.clean_data['first_name']
model.user.last_name = self.clean_data['last_name']
# more here...
model.save()
model.user.save()
it's violating the DRY principle, but at least it's only in one place,
the form Declaration, so it's easy to change. For more trivial use
cases (no compound models etc), I think that the form_for_model will
generate the same results.
On Mar 6, 11:10 pm, [EMAIL PROTECTED] wrote:
> I had similar problems, trying to tie the form and model too closely.>From
> what I understand the form needs to be aware of the model(s), but
>
> there isn't anything forcing you to a 1:1 relationship (form_for_model
> does that for convenience, but isn't the only way). You just have to
> override __init__() and save() for the form, and handle the model
> specific stuff there. I'm just starting with Django though, so
> hopefully someone more knowledgeable will chime in and either confirm
> or show us the right way!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---