2017-12-31 292 views
0

사용자 정의 필드를 만들려고합니다. postgres.JSONField을 기반으로합니다.get_prep_value()는 value = None을 처리해야합니까?

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을 읽었으며 최소한 코드 예제에서는 그렇지 않습니다. 아무도 여기서 무엇이 잘못 될지 설명 할 수 있습니까?

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) 
+1

예, 당신이 경우 None''처리해야 : 나는이 분야에 대한 null=True 설정 한 이후

get_prep_valueNone 처리해야 : – solarissmoke

+0

'value = None'이라면 어떻게해야합니까? – Sahand

+1

'None'을 반환해야합니다. – solarissmoke

답변

0

는이 솔루션을 @solarissmoke 감사 필드에 null 값을 허용합니다 (당신은 필드에`null = True`를 설정했습니다.)