im在制作这样的内部联接查询集时尝试获取所有字段:
querysetd = MenuResponseModel.objects.select_related('option').filter(MenuID=menuCreateId)
但是它只会用主表字段来回应我。
{'id':2,'option_id':7,'userName':'miguel','MenuID_id':UUID('057e4185-c79d-40ff-b90d-0bee254754c8'),'comments':'sin cebolla por preferred','date':datetime.date(2019,3,13)}
这些是我的模特
class MenuResponseModel(models.Model):
option = models.ForeignKey(Menu, blank=False)
userName = models.CharField(max_length=50, blank=True )
MenuID= models.ForeignKey(MenuCreateModel, blank=False, on_delete=models.CASCADE)
comments = models.CharField(max_length=200)
date = models.DateField(auto_now_add=True, blank=True)
class Menu(models.Model):
option = models.IntegerField(unique=True)
description = models.CharField(max_length=100)
date = models.DateField(auto_now_add=True, blank=True)
MenuID= models.ForeignKey(MenuCreateModel, default=uuid.uuid4, on_delete=models.CASCADE)
感谢帮助-
答案 0 :(得分:3)
您的ForeignKey字段中的值也存在于Queryset中。如果要查看这些内容,可以使用queryset的values方法:
querysetd.values('option__some_field', 'option__some_field2') # and so on
或直接访问以下值:
for i in querysetd:
i.option.some_field # will not hit database as you have used select related