2013-03-28 211 views
0

我試圖尋找在DB的項目。 db中有兩個項目,但我不知道怎麼做第二個項目。與我的代碼低於結果只是第一行Bewertung,但不是第二行。Django的模型 - queryset的問題

我的代碼很簡單:

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

什麼都可以的,爲什麼我找不到分貝第二項的原因是什麼?我得到第一個記錄,其中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 

這些都是記錄:

enter image description here

+2

你能在這裏發表您整個模型? – kender 2013-03-28 08:35:06

+0

怎麼樣'bewertung = Bewertung.objects.filter(bewertung__contains = INT(int_bew))' – catherine 2013-03-28 08:52:48

+0

你真的應該用英文寫代碼。在這樣的論壇,其中之一就是獲得幫助時(加蟒蛇已經在英語!)和「int_bew」不是很描述性的名稱有很大幫助,一個更好的名字是「bew_pk」(PK ==主鍵,這是標準的python符號) – boxed 2013-03-28 08:59:30

回答

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) 
+0

謝謝,但我已經這樣做了 – doniyor 2013-03-28 08:50:53

+0

我試試你的代碼過濾和它的作品在我身上。也許你錯過了你的代碼中的東西。 – catherine 2013-03-29 06:50:03

+0

哇,感謝您的指導。我現在會改變我的模型。 – doniyor 2013-03-29 07:10:55