Reputation: 1303
Brand new to Django so excuse the newbie question. I cannot for the life of me get a Google search to return what I need.
First, I imported these using inspectdb.
Second, before providing the:
def __unicode__(self):
return u'%s %s' % (self.id, self.cuisine)
in the models, every database showed what looked like bound objects versus the actual data when looking in the admin. I assumed this was normal.
Now I'm trying to query the database and show the results. Just doing something simple, the code is:
def expand(request):
userid = Userid.objects.filter(name__contains="Test")
return render(request,'expand.html',{'userid':userid})
The return should just be Test 1, Test 2, but instead I get:
[<Userid: Userid object>, <Userid: Userid object>]
Tried userid, userid.name in the template and both return the object versus the contents.
Thanks, sorry for what I'm sure is a repeat question!
Model:
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'
Template:
<p>{{ userid }}</p>
Upvotes: 1
Views: 412
Reputation: 99620
You need to parse through the object elements individually.
Like this:
{% for user in userid %}
{{user}}
{% endfor %}
--OR--
{% for user in userid %}
{{user.name}}
{% endfor %}
The __unicode__
attribute is applied only to individual objects, and not a queryset. Hence the issue.
Make sure your __unicode__
is on the Userid
model.
If you want to just display the name list, you can alternatively do
def expand(request):
userid = Userid.objects.filter(name__contains="Test").values_list('name', flat=True)
return render(request,'expand.html',{'userid':", ".join(list(userid))})
and your template would be just:
{{userid}}
Upvotes: 6
Reputation: 174624
In your Userid
model, add the __unicode__
method:
class Userid(models.Model):
# all your fields
class Meta:
db_table = 'userID'
def __unicode__(self):
return unicode('{0} {1}'.format(self.name, self.id))
Upvotes: 1