paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
390 stars 67 forks source link

Relation Not Created For Reverse Many To One Managers #93

Closed bisby closed 4 years ago

bisby commented 6 years ago

With models like such:

class User(models.Model):
   name = models.CharField(max_length=100)

class Comment(models.Model):
   text = models.TextField()
   user = models.ForeignKey(User, related_name="comments")

I would hope that I could run G(User, comments=[F(), F()]) to make a user with a few test comments.

However, this creates a user, but creates no comments.

G(Comment, user=F()) creates a comment, and a user.

The solution is to do

x = G(User)
comments = [G(Comment), G(Comment)]
x.comments.set(comments)

But this seems extraneous when F() exists for a reason. Would there be a way to make it so reverse relation managers behave the same way that a regular relation manager works?

paulocheque commented 6 years ago

That would be an awesome PR.

For now, another possibility is:

u = G(User) ; G(Comment, user=u, n=2)