1

我有一个model和一个manageras

class PlaylistManager(models.Manager):
    def add_playlist(self, name):
        playlist = self.model(name)
        playlist.save()
        return playlist

class Playlist(models.Model):
    name = models.CharField(max_length=30)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    deleted = models.BooleanField(default=False)
    objects = PlaylistManager() # is a customer manager

    class Meta:
        db_table = 'playlists'

然后我写一个test以确保它有效

from django.test import TestCase
from models import Playlist

"""
The idea is that each call to create playlist will make a new playlist

"""

PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
    def insert_playlist(playlist=PLAYLIST):
        Playlist.objects.add_playlist(playlist)

    def test_add_one_video_to_playlist(self):
        self.insert_playlist()
        self.assertEqual(Playlist.objects.count(), 0, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))

当我从命令行运行此测试时,我看到以下错误

$ python manage.py test playlists --settings=myApp.settings.dev
Creating test database for alias 'default'...
EE
======================================================================
ERROR: test_add_one_video_to_playlist (myApp.apps.playlists.tests.PlaylistTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/code/p/myApp/myApp/apps/playlists/tests.py", line 15, in test_add_one_video_to_playlist
    self.insert_playlist()
  File "/Users/me/code/p/myApp/myApp/apps/playlists/tests.py", line 12, in insert_playlist
    Playlist.objects.add_playlist(playlist)
  File "/Users/me/code/p/myApp/myApp/apps/playlists/models.py", line 8, in add_playlist
    playlist.save()
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/base.py", line 524, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/query.py", line 621, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/query.py", line 639, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1099, in add_filter
    value = value()
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/test/testcases.py", line 512, in __call__
    result.addError(self, sys.exc_info())
AttributeError: 'NoneType' object has no attribute 'addError'

======================================================================
ERROR: test_add_one_video_to_playlist (myApp.apps.playlists.tests.PlaylistTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/test/testcases.py", line 508, in __call__
    self._post_teardown()
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/test/testcases.py", line 522, in _post_teardown
    self._fixture_teardown()
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/test/testcases.py", line 847, in _fixture_teardown
    transaction.leave_transaction_management(using=db)
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/transaction.py", line 52, in leave_transaction_management
    connection.leave_transaction_management()
  File "/Users/me/code/p/virtualenv/myApp/lib/python2.7/site-packages/django/db/backends/__init__.py", line 115, in leave_transaction_management
    raise TransactionManagementError("This code isn't under transaction "
TransactionManagementError: This code isn't under transaction management

----------------------------------------------------------------------
Ran 1 test in 0.102s

FAILED (errors=2)
Destroying test database for alias 'default'...

我不知道这里发生了什么。我只知道我有一个Transaction Management Middleware as

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # -- db transaction management -- #
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.transaction.TransactionMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

计划在我的views.py功能中 使用它

@transaction.commit_on_success
def addPlaylist(name):
    """
    adds playlist
    """
    pass

但目前没有调用任何视图函数

请帮助我这里发生了什么

谢谢

更新

我正在使用 Django 1.4

09:28:21  $ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 4, 0, 'final', 0)
>>> 
4

1 回答 1

1

将我更改PlaylistManager为关注后,事情开始起作用

class PlaylistManager(models.Manager):
    def add_playlist(self, name):
        playlist = Playlist(name=name) # this line changed
        playlist.save()
        return playlist
于 2012-07-18T16:36:51.720 回答