疑难解答“相关字段包含无效查询:icontains”

时间:2012-08-01 07:53:38

标签: exception django-admin

我在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']

9 个答案:

答案 0 :(得分:126)

您是否尝试在__fieldname search_fields中的Lingua个引用中添加ListinoTraduttoreAdmin,例如:

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

答案 1 :(得分:58)

这是(希望)简化答案。

不要在ForeignKey字段本身上过滤


改变这个

search_fields = ['foreinkeyfield']

to(注意两个下划线)

search_fields = ['foreinkeyfield__name']

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

希望这有帮助

答案 2 :(得分:49)

确保您没有直接向search_field添加任何Foreignkey或ManyToManyField。

使用Django的双下划线约定。 docs

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

答案 3 :(得分:3)

需要双下划线

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException   
{  
  int length = attributes.getLength();
    for (int i=0; i<length; i++) {

    String name = attributes.getQName(i);

    if(name=="customer-no") {
    System.out.println("Customer-no:" + attributes.getValue(i));
    }
      }
}  


public void endElement(String uri, String localName, String qName) throws SAXException   
{  
}  

public void characters(char ch[], int start, int length) throws SAXException   
{  
    }
} 
};  
saxParser.parse("Customer_Payments1.xml", handler);

}   
catch (Exception e)   
{  
e.printStackTrace();  
}  

答案 4 :(得分:0)

这对我有用。

使用my_related_object__first_attribute搜索外键的字段:

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

答案 5 :(得分:0)

当您尝试使用ForeignKey进行筛选时,通常会发生此错误。我认为错误是在search_filelds中。检查。 search_fields = ['traduttore__nome“,” linguaDa“,” linguaA“]。这两个ForeignKey(” linguaDa“,” linguaA“)是问题。请将它们删除。我认为这会有所帮助。

答案 6 :(得分:0)

这可能无法回答最初的问题,但是我经常碰到类似的invalid lookup错误,因为我在查找中不小心使用了_set,例如<model_name>_set,而不只是<model_name>

基本上,我倾向于将related_query_namedefault_related_name混淆 ,其中确实包含_set(另请参见queries docsrelated manager docs)。

来自lookups documentation

它也向后工作。尽管可以自定义,但默认情况下,您会使用模型的小写字母在查找中引用“反向”关系。

(我的重点)

令人困惑的是,默认 related_name(即<model_name>_set)与默认 related_query_name(即<model_name>,但如果您设置 custom related_name(或通过模型default_related_name选项设置Meta,则将会也可用作默认的related_query_name(如文档中所述)。

答案 7 :(得分:0)

可能有点奇怪

search_fields = ['traduttore__nome']

像这样使用单引号会产生错误。

search_fields = ["traduttore__nome"]

使用双引号可以解决问题

foreignkeyfield__lookupfield  - this is the format

答案 8 :(得分:-1)

添加admin.py

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

请参阅链接https://docs.djangoproject.com/en/dev/intro/tutorial02/