简体   繁体   English

提取字段表单Django模型

[英]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. 现在,我要提取我保存在数据库中的特定用户ID的状态字段的值。 I am doing it currently in this way: 我目前正在以这种方式进行操作:

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

Which gives me a Query Set, eg: 这给了我一个查询集,例如:

<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? 那么,在此示例中,获取字段字符串“某些状态”的正确方法是什么?

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM