简体   繁体   English

Django:使用Model-Mommy测试自定义ModelForm的“清理”方法

[英]Django: Testing the 'clean' method of a custom ModelForm using Model-Mommy

======= Update ======= ======= 更新 =======

@JF brought forward a brilliant perspective for the use of model mommy in ModelForms. @JF提出了在ModelForms中使用模型妈妈的出色观点。

With the following code, I managed to automatically generate random data for all the fields of the model, except for the ones I would like to manipulate. 通过以下代码,我设法为模型的所有字段自动生成随机数据,但我想操纵的数据除外。

tests.py: tests.py:

from django.test import TestCase
from houses.forms import ManForm
from houses.models import Man

class ModelsTest(TestCase):

    def test_Man(self):
        man = mommy.make('Man', age=20)
        data = {each_field.name: getattr(man, each_field.name) for each_field in man._meta.fields}
        data.update({'age_verification': 20})
        form = ManForm(data)
        self.assertTrue (form.is_valid())

models.py models.py

from django.db import models

class Man(models.Model):
    name = models.CharField(max_length = 12)
    age = models.PositiveSmallIntegerField()

forms.py: forms.py:

from my_app.models import Man
from django import forms
from django.core.exceptions import ValidationError

class ManForm(forms.ModelForm):
    age_verification = forms.IntegerField()

    def clean(self):
        if not self.cleaned_data['age'] == self.cleaned_data['age_verification']:
            raise ValidationError("Is this a LIE?")

    class Meta:
        model = Man
        fields = ['name', 'age', 'age_verification']    

For the time being, I test it like this: 目前,我这样测试:

tests.py: tests.py:

from django.test import TestCase
from houses.forms import ManForm

class ModelsTest(TestCase):

    def test_Man(self):
        data = {
            'name' = 'John',
            'age' = 20,
            'ege_verification' = 20,
        }
        form = ManForm(data)

Is there a tool that provides random data for Forms? 是否有提供表格随机数据的工具? Or ... can I use the available tools for models for this purpose? 或者...我可以为此目的使用可用的模型工具吗? Searching the docs of those supporting Python 3, I did not understand how this could be achieved. 在搜索那些支持Python 3的文档时,我不知道该如何实现。

The only one that clearly provides such a service is Django-Whatever which is not python3 compatible. 唯一明显提供这种服务的是Django-Whatever ,它与python3不兼容。

If you don't want to type the name of each attribute, you can do this: 如果您不想键入每个属性的名称,则可以执行以下操作:

from model_mommy import mommy
from django.forms import model_to_dict

class ModelsTest(TestCase):

    def test_Man(self):
        man = mommy.make('Man')
        data = model_to_dict(man)
        form = ManForm(data)

You could do something like this: 您可以执行以下操作:

from model_mommy import mommy
class ModelsTest(TestCase):

    def test_Man(self):
        man = mommy.make('Man', _fill_optional=True)
        data = {
            'name': man.name,
            'age': man.age,
            'age_verification': man.age_verification,
        }
        form = ManForm(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM