How to handle 'self' Foreign key relation in factory boy
See original GitHub issueHello,
I’ve a django model where i’m using Foreign key with itself. Can you please guide me how i can replicate self relation in factory boy class. e.g.
class A(models.Model):
name = models.CharField(max_length=255)
parent = models.ForeignKey('self', blank=True, null=True)
Please guide how i can create a factory for such type of relations.
Thanks.
Issue Analytics
- State:
- Created 9 years ago
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Common recipes — Factory Boy stable documentation
Most recipes below take on Django model examples, but can also be used on their own. Dependent objects (ForeignKey) . When one...
Read more >Factory Boy subfactory over 'self' - Stack Overflow
To make it easier on the tools to introspect your model, instead of 'self', use the fully qualified model name: koppel_halte1 = models....
Read more >Setting Up A Factory For One To Many Relationships In ...
[Factoryboy] is used to replace fixtures with factories for complex objects. ... ForeignKey( 'team', # class name on_delete=models.
Read more >How To Create A Instance That Have Fk On User With Factory ...
The problem is testing this with factory boy has proven difficult and always results how to handle self referential foreign key relation in...
Read more >[AF] Factoryboy and Foreign Keys : r/flask - Reddit
ahh, ic. Factoryboy is in the title. I'm trying to build factories (UserFactory, PostFactory) based on my domain models (User, Post). I just...
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
Well, the simplest way would be to define a self-referential factory:
Then, you’ll just have to define the target depth when calling:
I’ve added a test on that topic, too.