lately I found a weird behavior in Django. Then, I start thinking that what’s wrong is the way I’m doing the work.
Lets assume that we have 2 models
class A(models.Model):
attr1 = models.CharField()
...
class B(models.Model):
a = models.ForeignKey("A",
related_name = "bs"
blank = True,
null = True)
So, if I have some items of B, without associations to A, say b1, b2, b3, when I set a new instance of A, say a1, if I query
a.bs.all()
b1, b2 and b3 are returned.
What’s wrong with it? When I create a new instance, it should not rise any relation. I know that b1,b2 and b3 have no relations, but they can’t be associated by default to any new instance.
Anyone know how to proceed in a Django way?
I know I can do the trick
if a1.id:
return []
else:
a1.bs.all()
But I think that’s not the right way to do this.
Can anyone help me?
Thank you in advance
This is a bug in Django and is currently marked as “design decision needed”:
https://code.djangoproject.com/ticket/14615