我已覆盖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")
答案 0 :(得分:1)
def test_form_saves_values_to_instance_user_on_save(self):
"""
test form saves name, surname, email values to corresponding User object
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(person.user.first_name, "has_changed")
else:
self.fail("personform not valid")
我认为您还需要从表单中排除user
字段
class PersonForm(ModelForm):
class Meta:
model = Person
exclude = ('user',)
...
答案 1 :(得分:1)
将新数据作为字典传递给表单(作为第一个位置参数或data
),并断言personform.cleaned_data['name']
等于User.objects.get(pk=user.pk).username
。然后对surname
和email
执行相同的操作。
请注意,first_name
和last_name
的{{1}}为30,而您的表单字段的max_length
为100。