我正在尝试使用Django-nested-inlines
应用程序,但它会引发错误。这是一个简单的代码:
MODELS.PY
class Language_Quiz(models.Model):
name = models.CharField(max_length=40)
language = models.OneToOneField(sfl_models.Language)
class Question(models.Model):
language_quiz = models.ForeignKey(Language_Quiz)
text = models.TextField()
class Answer(models.Model):
question = models.ForeignKey(Question,related_name='answers')
text = models.TextField()
correct = models.BooleanField()
ADMIN.PY
from django.contrib import admin
import models
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline
class AnswerInline(NestedTabularInline):
model = models.Answer
class QuestionInline(NestedTabularInline):
model = models.Question
class LanguageQuizAdmin(NestedModelAdmin):
inlines = [QuestionInline]
admin.site.register(models.Language_Quiz,LanguageQuizAdmin)
admin.site.register(models.Answer)
admin.site.register(models.Question)
但是当我运行服务器或尝试进行迁移时,会引发错误。我不知道问题出在哪里。问题在哪里?
TRACEBACK
(venv) C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2>python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
utility.execute()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\management\__init__.py", line 328, in execute
django.setup()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\apps\registry.py", line 115, in populate
app_config.ready()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\admin\apps.py", line 22, in ready
self.module.autodiscover()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\utils\module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\language_tests\admin.py", line 3, in <module>
from nested_inlines.admin import NestedModelAdmin, NestedTabularInline
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\nested_inlines\admin.py", line 16, in <module>
class NestedModelAdmin(ModelAdmin):
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\nested_inlines\admin.py", line 131, in NestedModelAdmin
@transaction.commit_on_success
AttributeError: 'module' object has no attribute 'commit_on_success'
答案 0 :(得分:0)
正如Kapil Sachdev和karthikr评论的那样,问题在于兼容性。 Django-nested-inlines 可能在使用Django 1.8 +时遇到了一些问题。
就我而言,解决方案非常简单。使用pip
Django-nested-inline
下载(在'内联'末尾没有's'),只需更改导入即可。我甚至不需要更改代码。