django_get_or_create with SubFactory
See original GitHub issueDescription
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/django.py:151: 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)
Notes
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:
- Created a year ago
- Comments:6 (5 by maintainers)
Top 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 >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
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{}
?Thanks for providing some context! Checking for
None
will be the fix over here then at this moment I think. Raising theIntegrityError
gives the user enough information to fix it I suppose (at least it would have give me).