web-dev-qa-db-fra.com

Dépannage "Le champ connexe a une recherche non valide: icontains"

J'ai les modèles suivants dans 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"

Dans le fichier admin.py, j’ai les informations suivantes:

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"]

Mais lorsque j'essaie de faire une recherche dans la page d'administration de la table ListinoTraduttore, l'erreur suivante apparaît:

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']
77
user1545895

Avez-vous essayé d'ajouter le __fieldname sur ces Lingua références dans le ListinoTraduttoreAdmin search_fields, comme:

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

C'est pour (espérons) simplifier la réponse.

Ne pas filtrer sur un champ ForeignKey lui-même!


Change ça

search_fields = ['foreinkeyfield']

to (notez DEUX soulignés)

search_fields = ['foreinkeyfield__name']

name représente le nom du champ de la table avec laquelle nous avons une relation ForeinKey.

J'espère que cela t'aides

69
Dror

Assurez-vous de ne pas ajouter directement de Foreignkey ou de ManyToManyField à votre champ de recherche.

Utilisez plutôt la convention de double soulignement de Django. docs

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