SoFunction
Updated on 2024-11-10

Configuring search field in django admin when it is a foreign key

python 2.7.11

django 1.8.4

ERROR CONTENT:related Field has invalid lookup: icontains

I originally defaulted to thinking that when dealing with foreign key searches, django would automatically search the row data for that foreign key after str()ing it, but that's not really the case, and you need to explicitly write it out if you're adding the foreign key to the search field.

This is the solution I found from the internet and tested to work as follows:

file

# coding:utf8
from  import models

class Book():
  
  name = (max_length=255)
  title = (max_length=255)
  price = ()
  ...
  
class Category():
  
  CATEGORY_CHOICES = (
    ('00', 'English'),
    ('01', 'Computer'),
  )
  
  book = (Book)
  category = (max_length=255, choices=CATEGORY_CHOICES)
  remark = (max_length=255)
  ...

Documentation (category as an example)

# coding: utf8
from  import admin
from django import forms

from .models import Category

class CategoryAdmin():

  search_fileds = ('book__name', 'book__title', 'book__price', 'category') # Set the scope of the search field, if there is a foreign key, indicate which field of the foreign key, double underlined
  list_display = ('book', 'category') # The field to display on the page, or the value returned in __unicode__(self) if not set.
  list_display_links = ('category') # Set which fields on the page can be clicked to go to the detail page
  fields = ('category', 'book') # Set which fields are displayed when adding/modifying details, where the remark field will not be displayed
  
(Category, CategoryAdmin)

[ Description ]

The error "related Field has invalid lookup: icontains" may appear when using the search in the Django admin system, mainly due to the fact that foreign key lookups require the specification of the corresponding field.

The foreign key should not just be a mod, but should be an explicit field of another table.

So we need to specify the specific field "Foreign key field for this table __Field to be queried for the table where the foreign key is located".

Additional knowledge:Django using foreign key in model but showing xxx_object on page?

The following two models A,B are defined in Django:

class A:
   name=(max_length=15)
   def __unicode__(self):
       return 
 
class B:
   name=(max_length=15)
   f=(A,on_delete=models.DO_NOTHING)
 
   def __unicode__(self):
      return 

Note: I am using python 2.7 in the test environment, after everything is done and deployed to the cloud server, I see in the backend management system that B's attribute column f shows as A_Object, and does not show the value of A's attribute - name.

The reason for this is that I'm using python 3.6 on my cloud server, where I have to replace the __unicode__() method with the __str__() method to get it to work.

class A:
   name=(max_length=15)
   def __str__(self):
       return 
 
class B:
   name=(max_length=15)
   f=(A,on_delete=models.DO_NOTHING)
 
   def __str__(self):
      return 

Of course, if you are using a version that is, for better compatibility, you can use the @python_2_unicode_compatible decorator to make it handle Unicode characters the same way.

from  import python_2_unicode_compatible
 
@python_2_unicode_compatible
class A:
   name=(max_length=15)
   def __str__(self):
       return 
 
@python_2_unicode_compatible
class B:
   name=(max_length=15)
   f=(A,on_delete=models.DO_NOTHING)
 
   def __str__(self):
      return 

The above article in django admin configure the search field is a foreign key when the processing method is all I have shared with you, I hope to be able to give you a reference, and I hope that you can support me more.