如何测试 ModelForm save() 方法保存模型中的更改?

How to test ModelForm save() method saves changes in model?

我已经覆盖 AdminModelModelForm 以使 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)

更新测试

upd解决方案。原来我有未填充的请求不可见字段'user'。排除使表格有效并通过测试。

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")
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',)
    ...

将新数据作为字典传递给您的表单(作为第一个位置参数或作为 data),并断言 personform.cleaned_data['name'] 等于 User.objects.get(pk=user.pk).username。然后对 surnameemail 执行相同的操作。

请注意,first_namelast_namemax_length 为 30,而您的表单字段的 max_length 为 100。