“ RelatedField 有无效查找: 图标”故障排除

我在 models.py有以下型号:

class ListinoTraduttore(models.Model):
traduttore = models.ForeignKey('Traduttore', related_name='Traduttore')
linguaDa = models.ForeignKey(Lingua, related_name = "linguaDa")
linguaA = models.ForeignKey(Lingua, related_name = "linguaA")
prezzoParola = models.CharField(max_length=50, blank=True)
prezzoRiga = models.CharField(max_length=50, blank=True)
scontoCat = models.CharField(max_length=50, blank=True)
scontoFuzzy = models.CharField(max_length=50, blank=True)
scontoRipetizioni = models.CharField(max_length=50, blank=True)
class Meta:
verbose_name_plural = "Listini Traduttori"
def __unicode__(self):
return u"%s Da %s A %s Parola=%s Riga=%s ScontoCAT=%s ScontoFuzzy=%s ScontoRipetizioni=%s" % (self.traduttore, self.linguaDa, self.linguaA, self.prezzoParola, self.prezzoRiga, self.scontoCat, self.scontoFuzzy, self.scontoRipetizioni)




class Traduttore(models.Model):
nome = models.CharField(nomeString, max_length=50)
cognome = models.CharField(cognomeString, max_length=50)
nomeAzienda = models.CharField(nomeAziendaString, max_length=50, blank=True)
codiceFiscale = models.CharField(codiceFiscaleString, max_length=50, blank=True)
partitaIva = models.CharField(partitaIvaString, max_length=50, blank=True)
indirizzo = models.CharField(indirizzoString, max_length=50, blank=True)
telefono = models.CharField(telefonoString, max_length=50, blank=True)
fax = models.CharField(faxString, max_length=50, blank=True)
email = models.EmailField(max_length=50, blank=True)
referente = models.CharField(referenteString, max_length=50, blank=True)
valuta = models.ForeignKey(Valuta)
metodoPagamento = models.ForeignKey(MetodoPagamento)
datiBancari = models.CharField(datiBancariString, max_length=50, blank=True)
programmiUtilizzati = models.ManyToManyField(Programma, blank=True)
note = models.CharField(max_length=200, blank=True)
listino = models.ManyToManyField(ListinoTraduttore, related_name='listino', blank=True)
def __unicode__(self):
return u"%s %s %s" % (self.nome, self.cognome, self.nomeAzienda)
class Meta:
verbose_name_plural = "Traduttori"

admin.py中,我有以下几点:

class TraduttoreAdmin(admin.ModelAdmin):
list_display = ("nome", "cognome", "nomeAzienda")
search_fields = ["nome", "cognome", "nomeAzienda"]


class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa", "linguaA"]

但是,当我试图在 ListinoTraduttore表的管理页面中进行搜索时,出现了以下错误:

TypeError at /admin/itrad/listinotraduttore/
Related Field has invalid lookup: icontains
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/itrad/listinotraduttore/?q=Fenicio
Django Version: 1.4.1
Exception Type: TypeError
Exception Value:
Related Field has invalid lookup: icontains
Exception Location: /Library/Python/2.7/site-packages/django/db/models/fields/related.py in get_prep_lookup, line 142
Python Executable:  /usr/bin/python
Python Version: 2.7.2
Python Path:
['/Users/nicolac/Documents/DjangoProjects/mysite',
'/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
110123 次浏览

加入 admin.py

admin.site.register(Traduttore, TraduttoreAdmin)
admin.site.register(ListinoTraduttore, ListinoTraduttoreAdmin)

见链接 https://docs.djangoproject.com/en/dev/intro/tutorial02/

您是否尝试过在 ListinoTraduttoreAdmin search _ fields 中的那些 Lingua引用上添加 __fieldname,比如:

class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa__field1", "linguaA_field2"]

使用 Django 的双下划线约定。 < a href = “ https://docs.djangoproject.com/en/dev/questions/db/query/# lookups-that-span-relations”rel = “ noReferrer”> docs foreignkeyfield__name

Make sure you are not adding any Foreignkey or ManyToManyField to your search_field directly.

class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa__field1", "linguaA__field2"]

这是为了(希望)简化答案。

不要对 ForeignKey 字段本身进行过滤!


把这个换了

search_fields = ['foreignkeyfield']

为此(注意两个下划线)

search_fields = ['foreignkeyfield__name']

name表示与我们有 ForeignKey 关系的表中的字段名。

希望这个能帮上忙

这招对我很管用。

使用 my _ related _ object _ _ first _ tribute 搜索外键的字段:

search_fields = ('author__username', 'title')
from models
author = models.ForeignKey(User, on_delete=models.CASCADE,   related_name='blog_posts2')

需要双下划线

class exampleAdmin(admin.ModelAdmin):
search_field = ('yourforeignkeyname__choosefieldnameinyourforeignkey')

此错误通常发生在尝试使用 ForeignKey 进行筛选时。我认为错误出现在 search _ filfields 中。检查一下。Search _ fields = [‘ traduttore _ _ nome”,“ LanguaDa”,“ LanguaA”]。这两个 ForeignKey (“ language aDa”,“ language aA”)就是问题所在。把他们带走。我觉得这有帮助。

这可能不能回答最初的问题,但是,我偶尔会遇到类似的 invalid lookup错误,因为我在查找中不小心使用了 _set,例如,<model_name>_set而不仅仅是 <model_name>

基本上,我倾向于混淆 Related _ query _ name 相关的 _ query _ name和 < a href = “ https://docs.djangoproject.com/en/3.1/ref/model/options/# django.db.model. Options.default _ related _ name”rel = “ nofollow norefrer”> default _ related _ name ,其中确实包括 _set(也参见 查询文件相关经理文件)。

来自 查找文档:

它也能反过来工作。虽然它可以自定义,但默认情况下,您使用 模型的小写名称在查找中引用“反向”关系。

(我的强调)

令人困惑的是,违约 related_name(即 <model_name>_set)与 违约 related_query_name(即 <model_name>)并不相同,但如果你设置一个 <model_name>_set0 related_name(或 default_related_name,通过模型 Meta选项) ,那么 <model_name>_set1也被用作默认的 related_query_name(如文档中提到的)。

可能会很奇怪

search_fields = ['traduttore__nome']

像这样给出,单引号会产生错误。

search_fields = ["traduttore__nome"]

用双引号给予将解决这个问题

foreignkeyfield__lookupfield  - this is the format

请检查该字段是否存在于模型中,如果不存在,则从搜索列表中删除该搜索字段。 然后,检查搜索字段值是否是外键,如果有外键,那么请添加外键的字段名,如 ForeignKey _ _ FieldName (FieldName 正是您想要搜索的字段)。