根据model属性获取django对象id [英] Get django object id based on model attribute

查看:582
本文介绍了根据model属性获取django对象id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Places的基本模型,具有以下视图:

I have a basic model named "Places" which has this view:

def view_index(request, place_name):

用户将使用以下URL访问该视图:

The user will access that view with a URL like this one:

http://server.com/kansas



"kansas" is a value stored in a field named "name" inside the model "Places".

问题是我无法弄清楚如何根据对象名称获取对象id。有没有办法这样做?

The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this?

推荐答案

像这样:

place = Places.objects.get(name='kansas')
print place.id

这篇关于根据model属性获取django对象id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆