本文介绍了AttributeError:'module'对象没有属性'commit_on_success'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Django-nested-inlines 应用程序,但会引发错误。这是一个简单的代码:

I'm trying to use Django-nested-inlines application but it raises error. Here is a simple code:

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)

但是当我运行服务器或尝试进行迁移,出现错误。我不知道问题在哪里。问题在哪里?

But when I run server or try to make migrations, the error is raised. I have no idea where is the problem. Where is the problem?

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'


推荐答案

正如Kapil Sachdev和karthikr所言,问题是兼容性。 Django-nested-inlines 可能与Django 1.8 +有一些问题。

As Kapil Sachdev and karthikr commented, the problem is with compatibility. Django-nested-inlines has probably some issues working with Django 1.8+.

在我的情况下,解决方案非常简单。使用 pip Django-nested-inline ('in'结尾处没有')下载更改导入。我甚至没有更改代码。

In my case, the solution was very simple. Download using pip Django-nested-inline (without 's' at the end of the 'inline') and just change import. I didn't even have to change the code.

这篇关于AttributeError:'module'对象没有属性'commit_on_success'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:19