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.

django_get_or_create with SubFactory

See original GitHub issue


The django_get_or_create feature is working well when the concerned Factory is used as a BaseFactory but not when it’s used as a SubFactory When used as a SubFactory, the following error occurs : AttributeError: type object 'CurrencyFactory' has no attribute '_original_params'

To Reproduce

With the below code, the error will occur randomly when the same currency code/name is used.

To reproduce it systematically, just change the PairFactory with the following line :

base_currency = factory.SubFactory(CurrencyFactory, name="Bitcoin")

It will force the IntegrityError in the Currency Factory, so it will try the get_or_create feature. And then call the PairFactory() two times and voila.

E           AttributeError: type object 'CurrencyFactory' has no attribute '_original_params'

../../../.cache/pypoetry/virtualenvs/belfort-backend-KmGKt2wB-py3.10/lib/python3.10/site-packages/factory/ AttributeError
Model / Factory code
# Models
class Currency(models.Model):
    name = models.CharField(max_length=100, unique=True)
    symbol = models.CharField(max_length=20, unique=True)

class Pair(models.Model):
    base_currency = models.ForeignKey(
        Currency, on_delete=models.CASCADE, related_name="pairs_as_base"
    quoted_currency = models.ForeignKey(
        Currency, on_delete=models.CASCADE, related_name="pairs_as_quoted"

# Factories
class CurrencyFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = "Currency"
        django_get_or_create = ("name", "symbol")

    name = factory.Faker("cryptocurrency_name")
    symbol = factory.Faker("cryptocurrency_code")

class PairFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = "Pair"

    base_currency = factory.SubFactory(CurrencyFactory)
    quoted_currency = factory.SubFactory(CurrencyFactory)


I believe this is linked to the _generate() method which is not called for SubFactory, while the _original_params is set only in this method.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

foarsittercommented, Oct 23, 2022

Ok, took some time to provide a failing testcase (forgive me for the bad naming).

Do you agree that the problem can be solved by initiating the class attribute _original_params as an empty dict {}?

foarsittercommented, Oct 24, 2022

Thanks for providing some context! Checking for None will be the fix over here then at this moment I think. Raising the IntegrityError gives the user enough information to fix it I suppose (at least it would have give me).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does FactoryBoy create a new object from SubFactory ...
I believe the subfactory kicks in after the get_or_create irrespectively. Not sure of a s9lution though, it's not a use case I've come ......
Read more >
FactoryBoy creates a new object from SubFactory ... - GitHub
_create (this is where get_or_create is triggered). Return the generated object. Here, the problem is that, when the factory has a DJANGO_FACTORY_GET_OR_CREATE ...
Read more >
Common recipes — Factory Boy stable documentation
Most recipes below take on Django model examples, but can also be used on their ... (e.g a ForeignKey to another Model ),...
Read more >
Handling unique constraints with factory boy | JoeQuery
The django_get_or_create Meta option takes a tuple of field names you would like passed to Django's get_or_create . You should pass the This ......
Read more >
[Answered]-Django, Factory boy - can't create subfactory-django
[Answered]-Django, Factory boy - can't create subfactory-django ... User and Profile are associated as onetoone field, also Label is sub factory of Profile....
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 Post

No results found

github_iconTop Related Hashnode Post

No results found