Django全新,请原谅新手问题。我无法为我的生活获得Google搜索以返回我需要的内容。
首先,我使用inspectdb导入了这些。
第二,在提供之前:
def __unicode__(self):
return u'%s %s' % (self.id, self.cuisine)
在模型中,每个数据库在查看管理员时都会显示看起来像绑定对象的内容与实际数据。我认为这是正常的。
现在我正在尝试查询数据库并显示结果。只是做一些简单的事情,代码是:
def expand(request):
userid = Userid.objects.filter(name__contains="Test")
return render(request,'expand.html',{'userid':userid})
返回应该只是测试1,测试2,但我得到:
[<Userid: Userid object>, <Userid: Userid object>]
在模板中尝试了userid,userid.name,并返回对象与内容。
谢谢,对不起,我确信这是一个重复的问题!
型号:
class Userid(models.Model):
id = models.BigIntegerField(primary_key=True, db_column='ID') # Field name made lowercase.
name = models.TextField()
joindate = models.DateField(db_column='joinDate') # Field name made lowercase.
visits = models.IntegerField(null=True, blank=True)
gender = models.TextField(blank=True)
address = models.TextField()
address2 = models.TextField(blank=True)
addresscity = models.TextField(db_column='addressCity') # Field name made lowercase.
addressstate = models.TextField(db_column='addressState') # Field name made lowercase.
addresszip = models.IntegerField(db_column='addressZip') # Field name made lowercase.
rating = models.IntegerField()
lastvisit = models.DateField(null=True, db_column='lastVisit', blank=True) # Field name made lowercase.
topcuisine1 = models.IntegerField(null=True, db_column='topCuisine1', blank=True) # Field name made lowercase.
topcuisine2 = models.IntegerField(null=True, db_column='topCuisine2', blank=True) # Field name made lowercase.
topcuisine3 = models.IntegerField(null=True, db_column='topCuisine3', blank=True) # Field name made lowercase.
topcuisine4 = models.IntegerField(null=True, db_column='topCuisine4', blank=True) # Field name made lowercase.
topcuisine5 = models.IntegerField(null=True, db_column='topCuisine5', blank=True) # Field name made lowercase.
dealsparticipatedin = models.IntegerField(db_column='dealsParticipatedIn') # Field name made lowercase.
privateoffersparticipatedin = models.IntegerField(db_column='privateOffersParticipatedIn') # Field name made lowercase.
privateofferssent = models.IntegerField(db_column='privateOffersSent') # Field name made lowercase.
toprestaurant1 = models.IntegerField(db_column='topRestaurant1') # Field name made lowercase.
toprestaurant2 = models.IntegerField(db_column='topRestaurant2') # Field name made lowercase.
toprestaurant3 = models.IntegerField(db_column='topRestaurant3') # Field name made lowercase.
dob = models.DateField(null=True, blank=True)
tipsrating = models.IntegerField(null=True, db_column='tipsRating', blank=True) # Field name made lowercase.
visitsweekday = models.IntegerField(null=True, db_column='visitsWeekDay', blank=True) # Field name made lowercase.
visitsweekend = models.IntegerField(null=True, db_column='visitsWeekend', blank=True) # Field name made lowercase.
reviewrating = models.IntegerField(null=True, db_column='reviewRating', blank=True) # Field name made lowercase.
spendrating = models.IntegerField(null=True, db_column='spendRating', blank=True) # Field name made lowercase.
class Meta:
db_table = 'userID'
模板:
<p>{{ userid }}</p>
答案 0 :(得分:5)
您需要单独解析对象元素。
像这样:
{% for user in userid %}
{{user}}
{% endfor %}
- 或 -
{% for user in userid %}
{{user.name}}
{% endfor %}
__unicode__
属性仅应用于单个对象,而不应用于查询集。因此这个问题。
确保__unicode__
模型上的Userid
。
如果您只想显示名单,可以选择
def expand(request):
userid = Userid.objects.filter(name__contains="Test").values_list('name', flat=True)
return render(request,'expand.html',{'userid':", ".join(list(userid))})
,您的模板就是:
{{userid}}
答案 1 :(得分:1)
在Userid
模型中,添加__unicode__
方法:
class Userid(models.Model):
# all your fields
class Meta:
db_table = 'userID'
def __unicode__(self):
return unicode('{0} {1}'.format(self.name, self.id))