berinhard/model_mommy

Support NullBooleanField

berinhard opened this issue · 2 comments

I received this exception:

E           TypeError: <class 'django.db.models.fields.NullBooleanField'> is not supported by mommy.

Not setup atm to pull down and put a pull request in but the code below adds support. until this is fixed and released, you can add support using the custom fields functionality:

mommy.generators.add('django.db.models.fields.NullBooleanField', lambda: random.choice([None, True, False]))

full fix:

@ top of generators.py - add NullBooleanField to imports

from django.db.models import (
    CharField, EmailField, SlugField, TextField, URLField,
    DateField, DateTimeField, TimeField,
    IntegerField, SmallIntegerField, PositiveIntegerField,
    PositiveSmallIntegerField, BooleanField, DecimalField,
    FloatField, FileField, ImageField, IPAddressField,
    ForeignKey, ManyToManyField, OneToOneField, NullBooleanField)

a little further down, add entry to default_mapping

default_mapping = {
    ForeignKey: random_gen.gen_related,
    OneToOneField: random_gen.gen_related,
    ManyToManyField: random_gen.gen_m2m,

    BooleanField: random_gen.gen_boolean,
    NullBooleanField: random_gen.gen_nullboolean,
    #...
}

then in random_gen.py

def gen_nullboolean():
    return choice((True, False, None))