1

私は modelmanager

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 私が持っていることを 知っているすべて

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

しかし、現在、どのビュー関数も呼び出されていません

ここで何が起こっているのか助けてください

ありがとうございました

アップデート

Django1.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 に答える