09.02.2012 20:01, Rune Kaagaard пишет:
Maybe something like the following untested code:
def submit_if_any(a, b, conditions, submit_func):
has_any = False
for condition in conditions:
if condition(a, b):
has_any = True
break
if has_any:
submit_func(a, b)
def submit(a, b):
a.email = b['email'],
a.is_active = b['is_active']
submit_if_any(
user,
request['POST'],
(
lambda a,b: a.email == b['email'],
lambda a,b: a.is_active == b['is_active'],
),
submit
)
Hi, Rune.
First of all, the good way is using request['POST'].get('email') instead
of request['POST']['email']. This will prevent unnecessary exceptions.
Second, you can create ModelForm for your Model and use method save with
flag commit set as False. This method create model instanse for you, and
then you can compare it with old instance:
form = forms.Update(request.POST)
if form.is_valid():
obj = form.save(commit=False)
for field in filter(lambda x:x[0]!='_',user.__dict__.keys()):
if getattr(user, field, None)!=getattr(obj, field, None):
obj.save()
break
but maybe there is more cleaner solution.
--
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.