在电子学习平台https://github.com/PacktPublishing/Django-2-by-Example/tree/master/Chapter13/educa的代码中,我试图添加一个带有RelatedFieldWidgetWrapper和inlineformset的新记录(主题)。使用添加的代码,我得到错误“无属性_meta_get_field”。如何进行这项工作?
我更改了课程的admin.py,forms.py和views.py。这是我的代码和错误。
#admin.py
@admin.site.register(Subject, SubjectAdmin)
class SubjectAdmin(admin.ModelAdmin):
model = Subject
formset_factory = SubjectFormset
def __init__(self, model, admin_site):
super(SubjectAdmin, self).__init__(model, admin_site)
self.form.admin_site = admin_site
admin.site.register(Subject, SubjectAdmin)
#models.py
class Subject(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
class Meta:
ordering = ['title']
def __str__(self):
return self.title
class Course(models.Model):
owner = models.ForeignKey(User, related_name='courses_created', on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, related_name='courses', on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
overview = models.TextField()
created = models.DateTimeField(auto_now_add=True)
students = models.ManyToManyField(User, related_name='courses_joined', blank=True)
class Meta:
ordering = ['-created']
def __str__(self):
return self.title
#forms.py
SubjectFormset = inlineformset_factory(
Subject,
Course,
fields = ['title', 'overview'],
list_display = ['title', 'slug'],
prepopulated_fields = {'slug': ('title',)},
title = ModelMultipleChoiceField(queryset=Subject.objects.none()),
widgets = RelatedFieldWidgetWrapper(
FilteredSelectMultiple(('title'),False,),
Subject._meta_get_field('title').field.remote_field.model, admin_site))
class Media:
## media for the FilteredSelectMultiple widget
css = {
'all':('/media/css/widgets.css/',),
}
# jsi18n is required by the widget
js = ('/admin/jsi18n/',)
class Meta:
model = Subject
#views.py
class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin):
model = Course
formset_factory = SubjectFormset
#fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
#error
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x75a90468>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 120, in populate
app_config.ready()
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/apps.py", line 24, in ready
self.module.autodiscover()
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/usr/local/lib/python3.5/dist-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/action/educa/courses/admin.py", line 4, in <module>
from .forms import SubjectFormset
File "/home/action/educa/courses/forms.py", line 24, in <module>
Subject._meta_get_field('title').field.remote_field.model, admin_site))
AttributeError: type object 'Subject' has no attribute '_meta_get_field'