我正在編輯django.contrib.auth.forms.UserChangeForm
。基本上,auth_user的用戶編輯頁面。Django auth用戶的用戶更改表單上的super()保存方法
https://github.com/django/django/blob/master/django/contrib/auth/forms.py
根據源代碼,形式不具有save()
方法,所以應該從forms.ModelForm
繼承權?
對於完整的代碼,see here
class MyUserAdminForm(forms.ModelForm):
class Meta:
model = User
def __init__(self, *args, **kwargs):
super(MyUserAdminForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
if instance and instance.id: # username and user id
... the rest of the __init__ is setting readonly fields
.... some clean methods .....
def save(self, *args, **kwargs):
kwargs['commit'] = True
user = super(MyUserAdminForm, self).save(*args, **kwargs)
print user.username
print 'done'
return user
當我點擊保存,故稱'UserForm' object has no attribute 'save_m2m'
。我GOOGLE了很多,並嘗試使用add()
,但沒有奏效。什麼導致了這種行爲?
事情是:打印兩個print
語句。但該值從未保存到數據庫中。我認爲第二線已經保存了一次。
感謝
爲什麼你要替換此表單的保存方法? [信號](https://docs.djangoproject.com/zh/dev/topics/signals/)是否適合您? – danihp 2012-07-09 09:52:26
是的。我知道這個解決方案。實際上,信號對於這個特定用例來說是更好的選擇。我只是嘗試了超()。謝謝:))) – User007 2012-07-09 13:28:10