django queryset returns "classname object" instead of DB values
database, django, django-queryset, python
Solution
Here:
Rooms.objects.filter(room_state="emptyReady", id='1')
returns a queryset of `Room` objects.
Now, if you want a specific value:
Rooms.objects.filter(room_state="emptyReady", id='1').values_list('room_state', flat=True)
Read more on `values_list` here
Alternatively:
for d in Rooms.objects.filter(room_state="emptyReady", id='1'): print(d.room_state)
Another way is to specify a `__unicode__` attribute on `class Room`
class Rooms(models.Model,):
room_state = models.CharField(max_length=255, choices=[('emptyReady', 'empty'), ('emptyWaiting4Clean', 'clean ready'), ('busy', 'busy')])
room_type = models.IntegerField()
room_number = models.IntegerField()
def __unicode__(self):
return u'%s' % self.room_state
This should prin `room_state` value if `print d` is called
Problem
I have a problem with django queryset. models: ``` class Rooms(models.Model,): room_state = models.CharField(max_length=255, choices=[('emptyReady', 'empty'), ('emptyWaiting4Clean', 'clean ready'), ('busy', 'busy')]) room_type = models.IntegerField() room_number = models.IntegerField() ``` and I ty to check working of queryset by typing in python shell: ``` for d in Rooms.objects.filter(room_state="emptyReady", id='1'): print(d) ``` In my DB there is table Rooms_rooms, and inside there is column room_state, and if we make query on DB : ``` SELECT room_state FROM Rooms_rooms where id = 1 ``` return value is emptyReady But when I using queryset (like example above) I alwas get: Rooms object What I did wrong? I try to search answer in all WEB for a 3 hours, and my patience is now over so I count on You all ;)