Django admin模块使用search时报错:django.core.exceptions.FieldError: Related Field got invalid lookup: contains

日志如下:

<class 'django.core.handlers.wsgi.WSGIRequest'>
------------registered_admins: {'spaceCloud': {'imageinfo': <spaceCloud.space_admin.ImageInfoAdmin object at 0x103f02f60>, 'physicalnetinfo': <spaceCloud.space_admin.PhysicalNetInfoAdmin object at 0x103f02f98>, 'reposoftinfo': <spaceCloud.space_admin.RepoSoftInfoAdmin object at 0x103f100b8>, 'app': <spaceCloud.space_admin.AppAdmin object at 0x103f02eb8>, 'repositories': <spaceCloud.space_admin.RepositoriesAdmin object at 0x103f10080>, 'ippoll': <spaceCloud.space_admin.IPpollAdmin object at 0x103f02fd0>, 'containerinfo': <spaceCloud.space_admin.ContainerInfoAdmin object at 0x103f02ef0>, 'hostassociated': <spaceCloud.space_admin.HostAssociatedAdmin object at 0x103f02f28>, 'imageproject': <spaceCloud.space_admin.ImageProjectAdmin object at 0x103f10048>}, 'asset': {'server': <asset.space_admin.ServerAdmin object at 0x103f02c88>, 'asset': <asset.space_admin.AssetAdmin object at 0x103f02c50>, 'account': <asset.space_admin.AccoutAdmin object at 0x103f02c18>, 'role': <spaceAdmin.admin_base.BaseAdmin object at 0x103f02be0>}}
------------model_name: imageinfo
------------model_name: imageinfo
------------admin_class: <class 'spaceCloud.space_admin.ImageInfoAdmin'>
--------------filter_condtions {}
--------------------querysets,filter_condtions <QuerySet [<ImageInfo: 6.8>, <ImageInfo: v1.0_20180412_2353>, <ImageInfo: v1.0_20180412_2302>, <ImageInfo: v1.0_20180412_1452>, <ImageInfo: v1.0_20180412_1439>, <ImageInfo: v1.0_20180412_1237>, <ImageInfo: v1.0_20180412_1205>, <ImageInfo: v1.0_20180412_1157>, <ImageInfo: v1.0_20180412_1202>, <ImageInfo: v6.2_20180403_1130>, <ImageInfo: v1.0_20180412_1200>, <ImageInfo: v6.2_20180403_1125>, <ImageInfo: v6.2_20180403_1050>, <ImageInfo: v6.2_20180403_1033>, <ImageInfo: v6.2_20180403_1025>, <ImageInfo: v6.2_20180403_2211>, <ImageInfo: v6.2_20180403_2151>, <ImageInfo: latest>, <ImageInfo: v1.0_20180424_1154>, <ImageInfo: v1.0_20180419>, '...(remaining elements truncated)...']> {}
----------------admin_class.search_fields: ['project_name', 'repo_name', 'name']
------------------search_field: project_name
------------------search_field: repo_name
------------------search_field: name
------------------q_obj: (OR: ('project_name__contains', 'account'), ('repo_name__contains', 'account'), ('name__contains', 'account'))
Internal Server Error: /spacecloud/spacecloud_image/
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jianglinguo/PycharmProjects/Space/spaceCloud/views.py", line 94, in spacecloud_image
    view_data = admin_views.model_table_list(request,app_name,model_name,no_render=True)
  File "/Users/jianglinguo/PycharmProjects/Space/spaceAdmin/views.py", line 144, in model_table_list
    querysets,q_val = get_search_objs(request,querysets,admin_class)
  File "/Users/jianglinguo/PycharmProjects/Space/spaceAdmin/views.py", line 73, in get_search_objs
    search_results = querysets.filter(q_obj)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/query.py", line 784, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/query.py", line 802, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1250, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1270, in _add_q
    current_negated, allow_joins, split_subq)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1201, in build_filter
    raise FieldError('Related Field got invalid lookup: {}'.format(lookups[0]))
django.core.exceptions.FieldError: Related Field got invalid lookup: contains
[27/Apr/2018 08:34:33] "GET /spacecloud/spacecloud_image/?_q=account HTTP/1.1" 500 111242

 

表imageinfo,外键引用的字段(ForeignKey字段),admin模块添加search框出现的问题

class ImageInfo(models.Model):
    digest = models.CharField(max_length=128,blank=True,null=True)#"
    name = models.CharField(u'版本',max_length=32, null=True, blank=True) #v5.3_20180301",
    architecture = models.CharField(max_length=32,null=True,blank=True)#amd64",
    os = models.CharField(max_length=32, null=True, blank=True)#linux",
    docker_version = models.CharField(max_length=32,blank=True, null=True)#1.12.6",
    author = models.CharField(max_length=64 ,blank=True,null=True)#
    repo_name = models.ForeignKey("Repositories",blank=True,null=True)
    repo_addr = models.ForeignKey('RepoSoftInfo',max_length=64, blank=True, null=True ,default=1)

 

自定义的 admin,space_admin.py文件内容

修改前:
class ImageInfoAdmin(BaseAdmin):
    list_display = ['id','project_name','repo_name','name','docker_version','author','repo_addr','update_time']
    list_filter = []
    list_per_page = 15
    search_fields = ['project_name','repo_name','name'] #问题就在这里'project_name','repo_name'这两个是ForeignKey字段,直接取是取不到的会报错
   

修改后:
class ImageInfoAdmin(BaseAdmin):
    list_display = ['id','project_name','repo_name','name','docker_version','author','repo_addr','update_time']
    list_filter = []
    list_per_page = 15
    search_fields = ['project_name__name','repo_name__name','name'] #问题就在这里'project_name','repo_name'这两个是ForeignKey字段,应用跨表去取ForeignKey表里面的字段
   

修改后问题解决 OK 了

 

总结一下

    ForeignKey

    作为外键django默认是把Repositories类的id和project_name字段关联的,不能直接写project_name 具体字段,project_name是个对象,没法直接查询。

    project_name__name  双下线来查Repositories表里面的具体字段内容。

 

posted @ 2018-04-27 17:48  Brin.Guo  阅读(3975)  评论(0编辑  收藏  举报