我正在尝试创建自定义字段。它基于postgres.JSONField
。
class CardiosField(JSONField):
"""Field representing a models.Cardios object"""
def from_db_value(self, value, expression, connection):
if value is None:
return value
return parse_cardios(value)
def to_python(self, value):
if isinstance(value, models.Cardios):
return value
if value is None:
return value
return parse_cardios(value)
def get_prep_value(self, value):
cardios_pre_json = [serie_object.pre_json() for serie_object in value.series]
return json.dumps(cardios_pre_json)
我创建了一个包含此字段的模型:
class Workout(models.Model):
datetime = models.DateTimeField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
lifts = fields.LiftsField(null=True)
cardios = fields.CardiosField(null=True)
def __str__(self):
return str(self.datetime)+" "+self.user.email
__repr__ = __str__
我进行迁移没有问题,但是当我尝试迁移时,会发生这种情况:
(workout) Sahands-MBP:workout sahandzarrinkoub$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, workoutcal
Running migrations:
Applying workoutcal.0003_auto_20171231_2308...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 87, in database_forwards
field,
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 415, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 155, in column_sql
default_value = self.effective_default(field)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 229, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
prepared=False)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/sahandzarrinkoub/Documents/Programming/Web/Django/workout/workout/workoutcal/fields.py", line 52, in get_prep_value
cardios_pre_json = [serie_object.pre_json() for serie_object in value.series]
AttributeError: 'NoneType' object has no attribute 'series'
get_prep_value()
是否必须处理None
?我已经阅读了docs,但事实并非如此,至少在他们的代码示例中并非如此。谁能解释一下这里出了什么问题?
答案 0 :(得分:0)
感谢@solarissmoke提供此解决方案:
get_prep_value
必须处理None
,因为我已为该字段设置null=True
:
def get_prep_value(self, value):
if not value:
return value
cardios_pre_json = [serie_object.pre_json() for serie_object in value.series]
return json.dumps(cardios_pre_json)