Search code examples
pythondjangomodels

Extract field form django model


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?


Solution

  • 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'