为了账号安全,请及时绑定邮箱和手机立即绑定

Django 中的自动完成字段

Django 中的自动完成字段

神不在的星期二 2022-12-27 16:36:33
尝试在 Django 中设置 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)错误信息: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贡献1811条经验 获得超4个赞

正如错误消息所说:

您需要在Genre admin 中定义“search_fields”,因为您使用autocomplete_fields = ['genre'],

并且不要忘记向其注册 Genre 模型。


class GenreAdmin(admin.ModelAdmin):

  search_fields = ['title']


admin.site.register(Genre, GenreAdmin)


查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信