嘗試在 Django 中設(shè)置 autocomplete_fields。我有以下型號:from django.db import models class Genre(models.Model): title = models.CharField(max_length=255)class Movie(models.Model): title = models.CharField(max_length=255) year = models.IntegerField() time = models.CharField(max_length=255) director = models.CharField(max_length=255) genre = models.ManyToManyField(Genre) image = models.ImageField(upload_to='images/') actors = models.TextField() summary = models.TextField()admin.py:from django.contrib import adminfrom .models import Movie, Genreclass SettingAdmin(admin.ModelAdmin): search_fields = ['genre']class MovieAdmin(admin.ModelAdmin): autocomplete_fields = ['genre']admin.site.register(Movie, MovieAdmin)admin.site.register(Genre)錯(cuò)誤信息:Exception in thread django-main-thread:Traceback (most recent call last):File "c:\users\ali\appdata\local\programs\python\python38\lib\threading.py", line 932, in _ bootstrap_innerself.run()File "c:\users\ali\appdata\local\programs\python\python38\lib\threading.py", line 870, in runself._target(*self._args, **self._kwargs)File "C:\Users\Ali\Desktop\cinemaEnv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapperfn(*args, **kwargs)File "C:\Users\Ali\Desktop\cinemaEnv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_runself.check(display_num_errors=True)File "C:\Users\Ali\Desktop\cinemaEnv\lib\site-packages\django\core\management\base.py", line 441, in checkraise SystemCheckError(msg)django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:ERRORS:<class 'movies.admin.MovieAdmin'>: (admin.E040) ModelAdmin must define "search_fields", because it's referenced by MovieAdmin.autocomplete_fields.我使用 django.contrib.auth.models 中的用戶模型嘗試此代碼并且它有效。我使用 django.contrib.auth.models 中的用戶模型嘗試此代碼并且它有效。
1 回答

波斯汪
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
正如錯(cuò)誤消息所說:
您需要在Genre admin 中定義“search_fields”,因?yàn)槟褂胊utocomplete_fields = ['genre'],
并且不要忘記向其注冊 Genre 模型。
class GenreAdmin(admin.ModelAdmin):
search_fields = ['title']
admin.site.register(Genre, GenreAdmin)
添加回答
舉報(bào)
0/150
提交
取消