全新的Django,请原谅新手问题.我不能为我的生活得到谷歌搜索返回我需要的东西.
首先,我使用inspectdb导入这些.
第二,在提供之前:
def __unicode__(self):
return u'%s %s' % (self.id, self.cuisine)
Run Code Online (Sandbox Code Playgroud)
在模型中,每个数据库在查看管理员时都显示了看起来像绑定对象的内容与实际数据.我以为这是正常的.
现在我正在尝试查询数据库并显示结果.只是做一些简单的事情,代码是:
def expand(request):
userid = Userid.objects.filter(name__contains="Test")
return render(request,'expand.html',{'userid':userid})
Run Code Online (Sandbox Code Playgroud)
返回应该只是测试1,测试2,但我得到:
[<Userid: Userid object>, <Userid: Userid object>]
Run Code Online (Sandbox Code Playgroud)
在模板中尝试了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'
Run Code Online (Sandbox Code Playgroud)
模板:
<p>{{ userid }}</p>
Run Code Online (Sandbox Code Playgroud)
您需要单独解析对象元素.
像这样:
{% for user in userid %}
{{user}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
- 要么 -
{% for user in userid %}
{{user.name}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
该__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))})
Run Code Online (Sandbox Code Playgroud)
而你的模板只是:
{{userid}}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |