有一个模型和内联模型.. 我需要创建一个查询集,它从内联模型中检索数据并在模板中返回一个for循环。看起来像我的代码不工作但是任何人都可以帮忙吗?
class Account(models.Model):
uuid = ShortUUIDField(unique=True)
name = models.CharField(max_length=80)
desc = models.TextField(blank=True)
address_one = models.CharField(max_length=100)
def __str__(self):
return u"%s" % self.name
class Rates(models.Model):
airline = models.CharField(max_length=30, blank=True, verbose_name= 'AIRLINE')
dest = models.CharField(max_length=3,blank=True,verbose_name= 'DEST')
name= models.ForeignKey (Account, default = False, related_name="rates")
def __str__(self):
return self.airline
My for loop should retrieve all rates in case account name is XXX
def rates_acc(request, account):
account = Account.objects.get(name = xxx)
rates = [x for x in account.rates.all()]
context = {
'rates': Account.objects.filter (name__name__icontains = 'xxx'),
}
return render(request,'accounts/account.html',context)
答案 0 :(得分:1)
我的for循环应检索所有费率,以防帐号名称为XXX
然后这个:
'rates': Account.objects.filter(name__name__icontains='xxx)
应该是这样的
'rates': Rates.objects.filter(name__name='XXX')
或者,如果您想获得帐户名称包含XXX的所有费率
'rates': Rates.objects.filter(name__name__icontains='XXX')