我试图将列表添加到属性,但是当我尝试保存数据库时,将引发以下异常。我的代码有问题吗?之前,我修改了模型以添加新的属性saved_jadwal_ids。我以为它没有问题,因为我尝试在执行jadwal.save()之前为它设置值,然后可以打印该值。我是否真的需要进行迁移?谢谢。
jadwal = Jadwal.objects(id=jadwal_id).first()
eventIds = []
# courses = data['jadwals']
courses = getCourses()
for course in courses:
id = insert_event(calendar, course)
eventIds.append(id)
jadwal.saved_jadwal_ids = eventIds
jadwal.save()
Jadwal.py:
class Jadwal(Document):
user_id = ReferenceField('User')
jadwals = ListField(EmbeddedDocumentField(JadwalData))
created_at = DateTimeField(default=datetime.now)
saved_jadwal_ids = ListField(StringField)
错误:
Traceback (most recent call last):
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Anisha\RISTEK BISMILLAH\sunjadv2-server\app.py", line 631, in add_jadwal_to_calendar
jadwal.save()
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\mongoengine\document.py", line 362, in save
self.validate(clean=clean)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\mongoengine\base\document.py", line 377, in validate
field._validate(value)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\mongoengine\base\fields.py", line 234, in _validate
self.validate(value, **kwargs)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\mongoengine\fields.py", line 857, in validate
super(ListField, self).validate(value)
File "c:\users\anisha\appdata\local\programs\python\python37\lib\site-packages\mongoengine\base\fields.py", line 431, in validate
self.field._validate(v)
TypeError: _validate() missing 1 required positional argument: 'value'
答案 0 :(得分:1)
尝试更换:
saved_jadwal_ids = ListField(StringField)
具有:
saved_jadwal_ids = ListField(StringField())
我可以使用以下代码段重现并修复错误:
from mongoengine import Document, StringField, ListField
class Example(Document):
meta = {'collection': 'examples'}
saved_jadwal_ids = ListField(StringField) # should be StringField()
e = Example()
e.saved_jadwal_ids = ['not','ok']
e.save()
TypeError: _validate() missing 1 required positional argument: 'value'