question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DjangoModelFactory with mixins

See original GitHub issue

Hi 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:open
  • Created 9 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
rbarroiscommented, Oct 10, 2019

@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())
1reaction
aalvrzcommented, Jan 23, 2018

Was there any consensus on what is the best way to implement this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found