gpt4 book ai didi

python - Django : value too long for type character varying(30) when input not even 10 characters

转载 作者:行者123 更新时间:2023-12-01 05:58:21 32 4
gpt4 key购买 nike

我有一个以下

class PlaylistManager(models.Manager):
def add_playlist(self, name):
playlist = Playlist(name=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'

我的测试正在执行

PLAYLIST = 'playlist' # 8 characters
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(), 1, msg='playlist count is not 1, it is ' + str(Playlist.objects.count()))

当我测试这个时,我收到以下错误

DatabaseError: value too long for type character varying(30)


----------------------------------------------------------------------
Ran 1 test in 0.004s

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

问题
- 为什么会失败?
- 当我执行 max_length=300 并运行测试时,一切都很好,但这是不可取的。

更新

我使用 PostgreSQL 作为后端

<小时/>

更新1
这是测试中发生的情况:

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

更改了我的PlaylistManager一段时间来调试

class PlaylistManager(models.Manager):
def add_playlist(self, name):
logging.warn('type ' + str(type(name)))
logging.warn('adding ' + str(dir(name)))
playlist = Playlist(name=name)
playlist.save()
return playlist

情况-1:如果我说playlist=PLAYLIST
进入PlaylistManageradd_playlist的是一个PlaylistTest对象

Creating test database for alias 'default'...
WARNING:root:type <class 'vlists.apps.playlists.tests.PlaylistTest'>
WARNING:root:adding ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_addSkip', '_baseAssertEqual', '_classSetupFailed', '_cleanups', '_deprecate', '_fixture_setup', '_fixture_teardown', '_formatMessage', '_getAssertEqualityFunc', '_post_teardown', '_pre_setup', '_resultForDoCleanups', '_testMethodDoc', '_testMethodName', '_truncateMessage', '_type_equality_funcs', '_urlconf_setup', '_urlconf_teardown', 'addCleanup', 'addTypeEqualityFunc', 'assertAlmostEqual', 'assertAlmostEquals', 'assertContains', 'assertDictContainsSubset', 'assertDictEqual', 'assertEqual', 'assertEquals', 'assertFalse', 'assertFieldOutput', 'assertFormError', 'assertGreater', 'assertGreaterEqual', 'assertHTMLEqual', 'assertHTMLNotEqual', 'assertIn', 'assertIs', 'assertIsInstance', 'assertIsNone', 'assertIsNot', 'assertIsNotNone', 'assertItemsEqual', 'assertLess', 'assertLessEqual', 'assertListEqual', 'assertMultiLineEqual', 'assertNotAlmostEqual', 'assertNotAlmostEquals', 'assertNotContains', 'assertNotEqual', 'assertNotEquals', 'assertNotIn', 'assertNotIsInstance', 'assertNotRegexpMatches', 'assertNumQueries', 'assertQuerysetEqual', 'assertRaises', 'assertRaisesMessage', 'assertRaisesRegexp', 'assertRedirects', 'assertRegexpMatches', 'assertSequenceEqual', 'assertSetEqual', 'assertTemplateNotUsed', 'assertTemplateUsed', 'assertTrue', 'assertTupleEqual', 'assert_', 'client', 'client_class', 'countTestCases', 'debug', 'defaultTestResult', 'doCleanups', 'fail', 'failIf', 'failIfAlmostEqual', 'failIfEqual', 'failUnless', 'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises', 'failureException', 'id', 'insert_playlist', 'longMessage', 'maxDiff', 'restore_warnings_state', 'run', 'save_warnings_state', 'setUp', 'setUpClass', 'settings', 'shortDescription', 'skipTest', 'tearDown', 'tearDownClass', 'test_add_one_video_to_playlist']

所以它失败

**Case -2 : if I say `Playlist.objects.add_playlist('name')`**  
PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
def insert_playlist(playlist=PLAYLIST):
Playlist.objects.add_playlist('name') # this line has changed

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

一切看起来都不错!

Creating test database for alias 'default'...
WARNING:root:type <type 'str'>
WARNING:root:adding ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
.
----------------------------------------------------------------------
Ran 1 test in 0.007s

OK
Destroying test database for alias 'default'...

更大的问题

  • 为什么我们需要传递原始字符串?

我在这里感到困惑

最佳答案

这对我来说是一场灾难,我做得不对

我写的内容不正确且真实

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

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

我忘记将 self 添加到我的类方法中,这就是类实例一直被传递的原因

正确实现

PLAYLIST = 'playlist'
class PlaylistTest(TestCase):
def insert_playlist(self, playlist_name=PLAYLIST): # added self as first parameter here
Playlist.objects.add_playlist(playlist_name)

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

一切开始顺利!感谢所有回答并思考该问题的人

关于python - Django : value too long for type character varying(30) when input not even 10 characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11546302/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com