繁体   English   中英

django模型-queryset问题

[英]django model - queryset issue

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

我的代码很简单:

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

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

编辑

这就是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

这些是记录:

在此处输入图片说明

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM