AdminModel
の管理者変更フォームからフィールドを食べたり保存したりModelForm
できるように、 をオーバーライドしました。User
Person
class Person(models.Model):
user = models.OneToOneField(User)
#and also some char, text fields
@property
def name(self):
return self.user.first_name
@name.setter
def name(self, value):
self.user.first_name = value
#and by analogy surname and email properties
class PersonForm(ModelForm):
class Meta:
model = Person
name = forms.CharField(max_length=100, required=False)
surname = forms.CharField(max_length=100, required=False)
email = forms.EmailField(required=False)
def save(self, commit=True):
instance = super(PersonForm, self).save(commit)
user = instance.user
user.first_name = self.cleaned_data['name']
user.last_name = self.cleaned_data['surname']
user.email = self.cleaned_data['email']
user.save()
return instance
class PersonAdmin(admin.ModelAdmin):
fields = ['name', 'surname', 'email', 'and_others']
form = PersonForm
admin.site.register(Person, PersonAdmin)
しかし、私は save() メソッドをチェックするテストを書くのに苦労しています:
def test_form_saves_values_to_instance_user_on_save(self):
"""
test that, form saves name, surname, email values to corresponding User
when commiting form
"""
user = User.objects.get(username='admin')
person = Person.objects.get(user=user)
personform = PersonForm(instance=person, data={'name': 'has_changed'})
# if uncommented raisesValueError: The Person could not be changed
# because the data didn't validate.
# personform.save()
self.assertEquals("has_changed", User.objects.get(pk=user.pk).first_name)
更新テスト
ソリューションを更新します。リクエストされた非表示フィールド「ユーザー」を埋めていなかったことが判明しました。除外により、フォームが有効になり、テストに合格しました。
admin.py
class PersonForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
# ...
tests.py
def test_form_saves_values_to_instance_user_on_save(self):
"""
test that, form saves name, surname, email values to corresponding User
when commiting form
"""
person = Person.objects.get(user__username='admin')
personform = PersonForm(instance=person, data={'name': 'has_changed'})
if personform.is_valid():
person = personform.save()
self.assertEquals(User.objects.get(pk=person.user.pk).first_name, "has_changed")
else:
self.fail("personform not valid")