Factoryboy Django model with default fuzzy margins

I'm using a factoryboy to simulate a model like the one below and I'm wondering if there is a cleaner way that doesn't replicate all 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 the factory just deduced the numerical nature of the model fields and didn't need factory fields like

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

      

Is it possible?

+3


source to share


1 answer


I'm not sure if you can do this in factory

. But you can do it in one line withmodel_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')

      

+3


source







All Articles