Django Prefetch相关查询

时间:2017-10-12 13:18:16

标签: django django-queryset

我有两个模型Customer和Contact,我想在Contact模型中添加字段到Customer模型并访问它的值。如何解决? 提前谢谢。

models.py

class Customer(models.Model):
     code = models.CharField(primary_key=True,max_length=30)
     name = models.CharField(max_length=200)
     address = models.CharField(max_length=100)


class Contact(models.Model):
     partner_name =models.ForeignKey(Customer,blank=True,null=True,on_delete=models.CASCADE)
     contact_number=models.CharField(max_length=15,blank=True)
     email=models.EmailField(max_length=50,blank=True)

我试过了这个查询

q=Customer.objects.filter(name='xxx').prefetch_related('contact_set')

for i in q:
 print(i.contact_set)

返回

partner.Contact.None
partner.Contact.None
partner.Contact.None

1 个答案:

答案 0 :(得分:2)

您必须先使用compile 'com.google.firebase:firebase-auth:11.4.2'打印数据:

QuerySet.all()