Open dedy-purwanto opened 10 years ago
I somehow solved it by overriding declarations
in my mixin class, I'm not sure if it's the best solution.
class UserStampMixin(object):
@classmethod
def declarations(cls, extra_defs=None):
extra_defs = extra_defs or {}
user = UserFactory()
extra_defs.update(created_by=user, modified_by=user)
return getattr(cls, factory.base.CLASS_ATTRIBUTE_DECLARATIONS).copy(extra_defs)
Hi,
Have you tried putting the mixin after DjangoModelFactory
?
If this works, I'll add it to the recipes
section of the docs :)
I'm not sure if it's possible to put it after DjangoModelFactory
since all the mixin's method will only be called after the class on the left, so it will not work, I tried various ways to inject these extra attributes to make it work when the mixin is placed on the right, none of them works.
Anyway, here's the refined version which can accept user's arguments instead of alway overriding them by whatever specified in the mixin:
class UserStampMixin(object):
@classmethod
def _get_extra(self):
return dict(
created_by=UserFactory(),
modified_by=UserFactory(),
)
@classmethod
def declarations(cls, extra_defs=None):
extra = extra_defs or {}
# Don't override fields if passed by the user
extra.update({f: val for (f, val)
in cls._get_extra().items() if f not in extra.keys()})
return super(UserStampMixin, cls).declarations(extra)
Was there any consensus on what is the best way to implement this?
Was there any consensus on what is the best way to implement this?
I'm also curious
@danihodovic the solution should be using simple inheritance with a class Meta: abstract = True
in factories.
An example follows:
import collections
import factory
import factory.fuzzy
Car = collections.namedtuple('Car', ['brand', 'plate', 'mileage'])
Bus = collections.namedtuple('Bus', ['brand', 'plate', 'capacity'])
class VehicleFactory(factory.Factory):
class Meta:
abstract = True
brand = factory.Iterator(['peugeot', 'renault', 'delorean'])
plate = factory.fuzzy.FuzzyText(length=7)
class CarFactory(VehicleFactory):
class Meta:
model = Car
mileage = factory.fuzzy.FuzzyInteger(0, 10000)
class BusFactory(VehicleFactory):
class Meta:
model = Bus
capacity = factory.fuzzy.FuzzyInteger(20, 50)
print(BusFactory())
print(CarFactory())
Hi guys, I was wondering how to write factories that shares the same attributes, such as
created_by
andmodified_by
. Suppose I have 2 models:Car
andManufacturer
which has these 2 attributes, and in the Factory classes I wrote something like these:When accessing
CarFactory.attributes()
andManufacturerFactory.attributes()
, I get only{'title': '<sequence>'}
, so all my factory classes are throwing error because user stamp can't be null. Tried with several different ways but the results are just the same. Does Factory Boy support mixins?