Django: Cannot resolve keyword '' into field. Choices are:

梦想的初衷 提交于 2019-12-04 07:39:04

I think I have found the problem. I suppose the problem is with the name of my app which is __app__. Django field look up assumes everything before __(double underscore) is a field which in my case resolves to ``(empty string).

Always had hard time naming the default app and the project it lives in. Thought __app__ was more pythonic and clever solution. I guess I should rename my app to just app. Hope this works.

This can happen if you attempt to use __unicode__ or __str__ in places where Django expects a field name. In my case I was trying to use __unicode__, because I give my models meaningful implementations and wanted to reuse it in the first column of tables.

To get around this I added

class AdminBase(admin.ModelAdmin):

    def natural_title(self, obj):
        return unicode(obj)

    natural_title.short_description = 'Title'

where AdminBase is a custom base class for my admin classes. I can now use 'natural_title' as a field name and get the results I was seeking.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!