Hi Patrick,

On Mon, 2007-06-25 at 15:50 +0200, patrickk wrote:
> here´s my view (I think the form-class is not so important for my  
> question):
> 
>      if request.method == 'POST':
>          form = UserProfileForm(request.POST)
>          if form.is_valid():
>              # SAVING
>              user_profile.sex = form.cleaned_data['sex']
>              user_profile.date_of_birth = form.cleaned_data 
> ['date_of_birth']
>              user_profile.phone_number = form.cleaned_data 
> ['phone_number']
>              user_profile.place_address = form.cleaned_data 
> ['place_address']
>              user_profile.place_zip_code = form.cleaned_data 
> ['place_zip_code']
>              user_profile.place_location = form.cleaned_data 
> ['place_location']
>              if form.cleaned_data['place_state']:
>                  user_profile.place_state_id = int(form.cleaned_data 
> ['place_state'])
>              else:
>                  user_profile.place_state_id = ""
>              user_profile.save()
>              user_data.first_name = form.cleaned_data['first_name']
>              user_data.last_name = form.cleaned_data['last_name']
>              user_data.save()
>      else:
>          # INITIAL_DATA
>          initial_data = {'sex': user_profile.sex,
>              'first_name': user_data.first_name,
>              'last_name': user_data.last_name,
>              'date_of_birth': user_profile.date_of_birth,
>              'phone_number': user_profile.phone_number,
>              'place_address': user_profile.place_address,
>              'place_zip_code': user_profile.place_zip_code,
>              'place_location': user_profile.place_location,
>              'place_state': user_profile.get_state_id,
>          }
>          form = UserProfileForm(initial=initial_data)
> 
> problem is: I´d like to clean this view by eliminating SAVING and  
> INITIAL_DATA,

Good design question. You're right, there should be a way to make this
easier. You could define methods like get_initial_data on your model, as
you suggest. However, the drawbacks are that it will be a bit repetitive
if you have to do this for many models and it doesn't play nicely with
models from other applications (e.g. a model from Django's core) that
you can't really edit.

Another approach is to write a couple of functions that take a list of
fields and a model instance. For example (untested code):

        def do_save(form, model, fields):
           for field in fields:
              setattr(model, field, form.cleaned_data[field])
        
        def get_initial_data(model, fields):
           return dict([(f, getattr(model, f)) for f in fields])
        
If you were always going to be working with *all* the fields on the
model, you could even leave out the 'fields' parameter and get that
dynamically by iterating through model._meta.fields, but that is a
little more fiddly -- you might need to leave out auto_pk fields and
stuff like that.

Regards,
Malcolm

-- 
I don't have a solution, but I admire your problem. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to