2013-03-28 198 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