Trying to set up autocomplete_fields for ForeingKey in Django admin. I don't understand how correctly to set it up. I read the docs but there is not much information about autocomplete_fields, not enough for me at least.
admin.py
class AdminSettings(admin.ModelAdmin):
filter_horizontal = ('english_word', 'russian_word', 'turkish_word')
list_display = ['circassian_word', 'letter']
autocomplete_fields = ('circassian_word',)
search_fields = ['circassian_word__circassian']
models.py
class Word(models.Model):
circassian_word = models.ForeignKey(Circassian, blank=True, null=True, on_delete=models.CASCADE, verbose_name='Адыгэбзэ')
letter = models.ForeignKey(Alphabet, null=True, on_delete=models.CASCADE, verbose_name='Буква')
audio = models.FileField(upload_to='audio', blank=True, verbose_name='Озвучка')
turkish_word = models.ManyToManyField(Turkish, blank=True, verbose_name='Türkçe')
english_word = models.ManyToManyField(English, blank=True, verbose_name='English')
russian_word = models.ManyToManyField(Russian, blank=True, verbose_name='Русский')
Error message
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d07b9d8>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 410, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:
ERRORS:
<class 'psalale.admin.AdminSettings'>: (admin.E037) The value of 'autocomplete_fields[0]' refers to 'circassian_word__circassian', which is not an attribute of 'psalale.Word'.
System check identified 1 issue (0 silenced).
As it said in the docs:
You must define
search_fieldson the related object’sModelAdminbecause the autocomplete search uses it.
So you need to define
class CircassianAdmin(admin.ModelAdmin):
search_fields = [] # <- list of keys to search by
register it via decorator @admin.register(Circassian) or via admin.site.register(CircassianAdmin)
and remove search_fields attribute from AdminSettings
Friendly suggestion: rename AdminSettings to smth like WordAdmin, it is much more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With