2017-12-31 141 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を読みましたが、少なくともコード例ではそうではありません。誰でもここで何がうまくいかないのか説明できますか?

+1

はい、それはあなたの場合NONE' 'に対処する必要があります:私はフィールドのnull=Trueを設定しているので、

get_prep_valueNoneに対処する必要がありますフィールド上でヌル値を許可する(あなたはフィールド上で 'null = True'を設定しています) – solarissmoke

+0

' value = None'の場合はどうしたらいいですか? – Sahand

+1

'None' – solarissmoke

答えて

0

あなたは、このソリューションのために@solarissmokeありがとう:

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) 
関連する問題