Error when using search in Django admin module: django.core.exceptions.FieldError: Related Field got invalid lookup: contains

The log is as follows:

<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

 

Table imageinfo , the field referenced by the foreign key (ForeignKey field), the problem of adding the search box in the admin module

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)

 

Customized admin, space_admin.py file content

Before modification:
 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 ' ] #The problem is here 'project_name', 'repo_name' These two are ForeignKey fields, if you can't get them directly, an error will be reported
   

After modification:
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 ' ] #The problem is here 'project_name', 'repo_name' These two are ForeignKey fields, the
   

problem after applying cross-table to fetch the fields in the ForeignKey table solved ok

 

in conclusion

    ForeignKey

    As a foreign key, django associates the id of the Repositories class with the project_name field by default. You cannot write the specific field of project_name directly. Project_name is an object and cannot be queried directly.

    project_name__name Double offline to check the specific field content in the Repositories table.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325017755&siteId=291194637