Factory for Django model linking to self with related_name
See original GitHub issueGuys, Please advise how can I make factory with FactoryBoy 2.6.0 and python 2.7 from django model below . Each object may have ‘next’ object, and if some object is the ‘next’ for another object, then it knows this object by ‘previous’ attribute.
class MyModel(models.Model):
name = models.CharField('Name', max_length=128)
next = models.OneToOneField('self', related_name='previous',
on_delete=models.SET_NULL,
null=True, blank=True)
I tried following Factory:
class MyModelFactory(factory.django.DjangoModelFactory):
class Meta:
model = 'app.MyModel'
next = factory.SubFactory('pp.app.tests.factories.MyModelFactory', previous=None)
previous = factory.RelatedFactory('pp.app.tests.factories.MyModelFactory', 'next')
But, got the “RuntimeError: maximum recursion depth exceeded in cmp” error.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using related_name in Django - Stack Overflow
CharField(max_length=100) manufacturer = models. ... CASCADE) def __str__(self): return f"{self.id}, {self.name}" class Job(models.
Read more >Models - Django documentation
from django.db import models class Manufacturer(models.Model): # ... pass class Car(models.Model): manufacturer = models.ForeignKey(Manufacturer, on_delete= ...
Read more >Common recipes — Factory Boy stable documentation
Building the adequate link between two models depends heavily on the use case; factory_boy doesn't provide a “all in one tools” as for...
Read more >Testing Models with Django using Faker and Factory Boy
use the Meta class to tell your factory which model to use. We will create company factory in `core/factories/company.py`: import factoryclass CompanyFactory( ...
Read more >related_name - Django Built-in Field Validation - GeeksforGeeks
The related_name attribute specifies the name of the reverse relation from the User model back to your model.
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
It is not possible natively.
You could write a custom
ComplexParameter
for this:With this, your factory would look like this:
Could you try this and let us know:
If it’s useful, we’ll add it to the core 😃
The parameter method outlined above doesn’t seem to work with newer version of factoryboy (I am on
2.11.1
). In particular, if you rename thecompute
method toas_declarations
to fit in with the new way of declaring parameters thedeclaration
dictionary will not containfield_name
if you declare it as an argument.In the example above, running
MyFactory(next_depth=20)
will still give you a next depth of 10.I had a go at getting a toy version of this working another way:
This seems to work -
RecursiveFactory(20)
will give you a 20-deep recursion rather than 10-deep.@rbarrois: Is there still a way to do this with a parameter or similar? It would be nice to be able to separate the logic for handling recursion depth from the actual factory declaration.