0

我正在尝试在 db 中搜索一个项目。db 中有两个项目,但我无法以某种方式获得第二个项目。我的代码在下面,结果只是第一行,Bewertung而不是第二行。

我的代码很简单:

locations = Location.objects.all()[:5] 
bewertungs = Bewertung.objects.filter(von_location__in=locations)

为什么我在 db 中找不到第二个条目的原因是什么?我得到第一个记录bewertung是 4,但第二个没有出现在结果中。

编辑

这就是 Bewertung 模型。

class Bewertung(models.Model):
   von_location= models.ForeignKey(Location,related_name="locations_bewertung",default="")
   von_user = models.ForeignKey(User,related_name="users_bewertung",default="")
   price_leistung = models.IntegerField(max_length=5,default=00)
   romantic = models.IntegerField(max_length=3,default=00)
   bewertung = models.IntegerField(max_length=3,default=00)
   def __unicode__(self):
       return self.bewertung

这些是记录:

在此处输入图像描述

4

1 回答 1

1
class Bewertung(models.Model):
   //you don't have to put default="" because this is already required
   von_location= models.ForeignKey(Location,related_name="locations_bewertung")
   von_user = models.ForeignKey(User,related_name="users_bewertung")

   //use DecimalField instead of IntergerField
   //use max_digits not max_length because it is for string
   price_leistung = models.DecimalField(max_digits=3, decimal_place=2, default=0)
   romantic = models.DecimalField(max_digits=3, decimal_place=2, default=0)
   bewertung = models.DecimalField(max_digits=3, decimal_place=2, default=0)

   //you return your unicode with an int field which result to error 
   //so you must do it this way
   def __unicode__(self):
       return "{0}".format(self.bewertung)
于 2013-03-28T08:45:13.993 回答