簡體   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