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.

How to handle 'self' Foreign key relation in factory boy

See original GitHub issue

Hello,

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:closed
  • Created 9 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
Kub-ATcommented, Dec 18, 2019
class AFactory(factory.Factory):
    class Meta:
        model = models.A
    name =  factory.Sequence(lambda n: "a%d" % n)
    parent = factory.LazyAttribute(lambda x: AFactory(parent=None))
5reactions
rbarroiscommented, Nov 2, 2014

Well, the simplest way would be to define a self-referential factory:

# myproj/myapp/factories.py
class AFactory(factory.Factory):
    class Meta:
        model = models.A
    name =  factory.Sequence(lambda n: "a%d" % n)
    parent = factory.SubFactory('myproj.myapp.factories.AFactory')

Then, you’ll just have to define the target depth when calling:

a = AFactory(parent__parent__parent__parent=None)

I’ve added a test on that topic, too.

Read more comments on GitHub >

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

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