我的Models.py
如下所示。
class Payment(models.Model):
payment_id = models.SmallIntegerField(primary_key=True)
rental = models.ForeignKey('Rental', blank=True, null=True)
amount = models.DecimalField(max_digits=5, decimal_places=2)
payment_date = models.DateTimeField()
def __unicode__(self):
return u'%s' % (self.amount)
class Rental(models.Model):
rental_id = models.AutoField(primary_key=True)
inventory = models.ForeignKey(Inventory)
def __unicode__(self):
return u'%s' % (self.rental_id)
class Inventory(models.Model):
inventory_id = models.AutoField(primary_key=True)
film = models.ForeignKey(Film)
store = models.ForeignKey('Store')
last_update = models.DateTimeField()
def __unicode__(self):
return u'%s' % (self.amount)
我尝试使用以下代码显示给定rental_id
的{{1}}和payment
:
film_id
并且我很少得到预期的结果,如下所示:
for i in Rental.objects.filter(inventory__film_id=2).order_by("inventory_id"):
amnt=Payment.objects.filter(rental_id= int(i.rental_id))
print ("Rental of %s is %s") %(i, amnt)
帮我删除最终结果中的对象名Rental of 10310 is [<Payment: 4.99>]
Rental of 13421 is [<Payment: 4.99>]
Rental of 4364 is [<Payment: 4.99>]
Rental of 7733 is [<Payment: 9.99>]
Rental of 15218 is [<Payment: 9.99>]
预期结果:
Payment
答案 0 :(得分:1)
for i in Rental.objects.filter(inventory__film_id=2).order_by("inventory_id"):
amnt=Payment.objects.filter(rental_id= int(i.rental_id))
print ("Rental of %s is %s") %(i, amnt[0].amount)
由于付款对象已退回给您,您必须提及要显示的字段。