Creating objects for testing shouldn't hurt
model_mommy is a tool for creating objects for testing in Django, inspired in ruby's ObjectDaddy and FactoryGirl. It generate the values according with the field type, but i will add support to custom values as well.
pip install model_mommy
If you have a model like this in your app:
class Kid(models.Model): happy = models.BooleanField() name = models.CharField(max_length=30) age = models.IntegerField() bio = models.TextField() wanted_games_qtd = models.BigIntegerField() birthday = models.DateField() appointment = models.DateTimeField()
just call the mommy =):
from model_mommy import mommy from model_mommy.models import Kid kid = mommy.make_one(Kid)
and your object is created! No boring attributes passing like 'foobar' every damn time.
Import every model you need can be very boring, so, you can also call mommy using a string like this:
kid = mommy.make_one('model_mommy.Kid')
Note that you must use 'app_label.model_name'. The model_name is case insensitive.
mommy also handles relationships. Suppose the kid has a dog:
class Dog(models.Model): owner = models.ForeignKey('Kid')
when you do:
rex = mommy.make_one(Dog)
it will also create the Kid, automatically.
You can also specify values for one or more attribute.
another_kid = mommy.make_one(Kid, age = 3) assert another_kid.age == 3
But, if don't need a persisted object, mommy can handle this for you as well:
from model_mommy import mommy from model_mommy.models import Kid kid = mommy.prepare_one(Kid)
It works like make_one, but like was said, it doesn't persist the instance.
How mommy behaves?
model_mommy skips fields with null=True or blank=True. Also if the field has a default value, mommy will use it.
When you shouldn't let mommy do the things for you:
If you have a field that has any special validation, you should set the value by yourself. model_mommy should be used to handle the fields that doesn't have relation with the test that you're doing at the moment and don't require special validation(like unique, etc), but still required in order to create the object.
Doubts? Loved it? Hated it? Suggestions?
- vanderson.mota at gmail dot com
- italo.maia at gmail dot com
##Currently supports the fields: CharField, TextField, FloatField, ForeignKey, Date and DateTimeField, BooleanField, URLField, and all the integer-type Fields