On Feb 9, 9:41 am, Rune Kaagaard <rumi...@gmail.com> wrote:
> Dear django-users
>
> I keep doing patterns like:
>
>         has_changed = False
>         if resource.user.email != request.POST['email']:
>             resource.user.email = request.POST['email']
>             has_changed = True
>         if resource.user.is_active != request.POST['is_active']:
>             resource.user.is_active = request.POST['is_active']
>             has_changed = True
>         if has_changed:
>             resource.user.save()
>
> although it works, I feel like there is a cleaner solution. How would
> you solve such a problem?

https://docs.djangoproject.com/en/1.3/topics/forms/
https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/


Note that if you really insist on doing things by hand and not doing
any validation/sanitization on user inputs, you could at least avoid
repetitions:

user = resource.user
fields = ("email", "is_active")
has_changed = False
for field in fields:
    old = getattr(user, field)
    new = request.POST.get(field)
    if new != old:
        setattr(user, field, new)
        has_changed = True
if has_changed:
    user.save()

But I really don't see the point in NOT using forms :-/

-- 
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.

Reply via email to