0

I have defined the following model:

class User(models.Model):
    userid = models.CharField(max_length=26,unique=True)
    status = models.CharField(max_length=5)

Now I would like to extract the value of my status field for a specific userid that I have saved in my database. I am doing it currently in this way:

field = User.objects.filter(userid=id).values('status')

Which gives me a Query Set, e.g.:

<QuerySet [{'status': 'some status'}]>. 

However, I am not looking for the Query Set but for the Field string. So I am currently doing a workaround to get the field value:

field1 = str(field)
field2 = field1.replace("<QuerySet [{'status': '","")
field3 = field3.replace("'}]>","")

Which returns me: "some status". That is obviously super messy. So what would be the correct way to get the field string "some status" for this example?

robjeiter
  • 197
  • 1
  • 16

1 Answers1

4

If I've understood your problem, you can simply do as follows:

# Get your user
user = User.objects.get(userid=id)

# save the status
status = user.status

# print it!
print status # 'some status'
floatingpurr
  • 7,749
  • 9
  • 46
  • 106
  • 1
    The reason he is getting the said output in the first place is because it is returning an model object. To get the specific info, do as advised here by @floatingpurr – aspo Sep 14 '17 at 17:06
  • 1
    Thanks! This was exactly the answer I was looking for! – robjeiter Sep 14 '17 at 19:00