I have 2 Django models :
class A(models.Model):
uniq_name = models.CharField(max_length=30,primary_key=True)
info1 = models.CharField(max_length=30)
info2 = models.CharField(max_length=30)
class B(models.Model):
a = models.ForeignKey(A)
info3 = models.CharField(max_length=30)
info4 = models.CharField(max_length=30)
If I do :
b = B.objects.get(id = n), it generates one database request.
If I do
print b.a.pk : it generate another request.
Is that possible to access b.a primary key (I only need this information, not info1 nor info2) without generating another request nor using 'select_related()' ?
I could do a :
print b.__dict__['a_id']
It works, but it seems to me very ugly : do you have a nicer way ?
You don't need to go via the dict: b.a_id
works fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With