Django選項沒有_name_map屬性

[英]Django options has no attribute _name_map


I have a table in database:

我在數據庫中有一個表:

models.py

class Board(models.Model):
 ...

Now from python shell (manage.py shell), I can do:

現在從python shell(manage.py shell),我可以做到:

Board._meta._name_map

But I can't access it in my views.py:

但我無法在我的views.py中訪問它:

a = Board._meta._name_map

Error:

'Options' object has no attribute '_name_map'

There might be some other attributes missing too, so why is it?? How can I access this _name_map information?

可能還有其他一些屬性缺失,為什么呢?如何訪問此_name_map信息?

1 个解决方案

#1


1  

_name_map is a cache, not something you can rely on.

_name_map是一個緩存,而不是你可以依賴的東西。

I think you'll be better off with this:

我想你會因此而變得更好:

for field in Board._meta.fields:
    if isinstance(field, ForeignKey):
        k = field.name
        # then whatever

At least assuming you don't already know the name of the field you're looking for, since you say get_field_by_name doesn't help.

至少假設你還不知道你正在尋找的字段的名稱,因為你說get_field_by_name沒有幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/06/26/72f3f5e0df8bd278282e6e55db815795.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com