对 Django 来说是全新的,所以请原谅新手的问题。我一辈子都无法通过谷歌搜索返回我需要的东西。
首先,我使用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>