Search code examples
pythondjangofactory-boy

Factoryboy Django model with default fuzzyfields


I am using factoryboy to mock up a model like the one below, and I wonder if there is a cleaner way that doesn't involve replicating all the fields.

class ShippingContainer(models.Model):
    weight = models.IntegerField(null=False)
    objects = models.IntegerField(null=False)
    serial_number = models.IntegerField(null=False)


class ShippingContainerFactory(DjangoModelFactory):
    class Meta:
        model = ShippingContainer

    weight = FuzzyInteger(0, 500)
    objects = FuzzyInteger(0, 500)
    serial_number = FuzzyInteger(0, 500)

It would be nice if factoryboy just deduced the numeric nature of the model fields and didn't need the factory fields, such as

class ShippingContainerFactory(DjangoModelFactory):
    class Meta:
        model = ShippingContainer

is this possible?


Solution

  • I am not sure if you can do that in factory. But you can do it in a single line with model_mommy

    Install model_mommy.

    $ pip install model_mommy
    

    Create a dummy model.

    from model_mommy import mommy
    from your_app.models import ShippingContainer
    
    dummy_shipping_container = mommy.make('ShippingContainer')