DjangoModelFactory with mixins
See original GitHub issueHi guys, I was wondering how to write factories that shares the same attributes, such as created_by
and modified_by
. Suppose I have 2 models: Car
and Manufacturer
which has these 2 attributes, and in the Factory classes I wrote something like these:
class Car(models.Model):
title = models.CharField()
created_by = models.ForeignKey(User)
modified_by = models.ForeignKey(User)
class Manufacturer(models.Model):
title = models.CharField()
created_by = models.ForeignKey(User)
modified_by = models.ForeignKey(User)
class UserFactory(DjangoModelFactory)
FACTORY_FOR = User
class UserStampMixin(object):
@lazy_attribute
def created_by(self): return UserFactory()
@lazy_attribute
def modified_by(self): return UserFactory()
class CarFactory(UserStampMixin, DjangoModelFactory)
FACTORY_FOR = Car
title = Sequence(lambda n: "Car %03d" % n)
class ManufacturerFactory(UserStampMixin, DjangoModelFactory)
FACTORY_FOR = Manufacturer
title = Sequence(lambda n: "Manufacturer %03d" % n)
When accessing CarFactory.attributes()
and ManufacturerFactory.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?
Issue Analytics
- State:
- Created 9 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Wrong inheritance behavior in factory boy factories
So I found some workaround. Basically, I change inheritance order and repeat some code. Like this: FactoryMinimal(AbstractFactoryMinimal): ...
Read more >Common recipes — Factory Boy stable documentation
DjangoModelFactory ): class Meta: model = models.User first_name = factory.Sequence(lambda n: "Agent %03d" % n) group = factory.SubFactory(GroupFactory) ...
Read more >Create Reusable Models with Django and Mixins
Using mixins to create powerful, flexible Django models. ... How are Mixins different from using Interfaces and Abstract classes?
Read more >Can a class which inherits from factory.DjangoModelFactory ...
Hi,. I have a problem with tests. We defined a class called DefaultUserFactory which inherits from factory.DjangoModelFactory, and I want to use ...
Read more >Testing with Django REST Framework - DEV Community
import factory from factory.django import DjangoModelFactory class ... It has a client to authenticate a user so you could test mixins and ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@danihodovic the solution should be using simple inheritance with a
class Meta: abstract = True
in factories. An example follows:Was there any consensus on what is the best way to implement this?