0

======= Update =======

@JF brought forward a brilliant perspective for the use of model mommy in 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:

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

from django.db import models

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

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:

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.

The only one that clearly provides such a service is Django-Whatever which is not python3 compatible.

raratiru
  • 8,748
  • 4
  • 73
  • 113
  • Is `self.cleaned_data` equal to `None` in `clean()`? Is the `ValidationError` being raised? – Jed Fox Sep 13 '15 at 14:40
  • @JF I have just updated my question to reflect the main issue which is the random data generator. I apologize for that. The models work as expected, the no-validation issue, was my mistake. – raratiru Sep 13 '15 at 14:43

2 Answers2

2

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)
Georg Zimmer
  • 899
  • 1
  • 7
  • 13
1

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)
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
  • Thank you! I am looking for a more scalable solution where several fields can be randomly defined except for the ```age``` and ```age_verification``` fields where I should define them. – raratiru Sep 13 '15 at 14:52
  • What types of fields are you interested in randomly defining? – Jed Fox Sep 13 '15 at 14:53
  • I have a model which has CharFields, BooleanFields, IntegerFields and some CharFields have a choices attribute. [Model Mommy](https://github.com/vandersonmota/model_mommy), for example, does the job easily for the Model why not for a ModelForm? – raratiru Sep 13 '15 at 14:56
  • Thank you, this is great! It lacks the feature of auto-detection (I still have to hard code the name of each attribute) but it does the job. – raratiru Sep 13 '15 at 16:46