2017-01-20 7 views
1

Adminで「Placerating」のエントリを開き、フィールドを変更した後に保存しようとすると、Django管理者は「このフィールドは必須です」と表示します。フィールド「Pic」の上に表示されます。DjangoモデルOneToOneField: "このフィールドは必須です"エラー

class Placerating(models.Model): 
    theplace = models.ForeignKey('ThePlace', on_delete=models.CASCADE, null=True, related_name='placeratings') 
    pic = models.OneToOneField('fileupload.Picture', on_delete=models.SET_NULL, null=True) 
    def __unicode__(self): 
      return self.theplace.name 

class Picture(models.Model): 
    def set_upload_to_info(self, path, name): 
     self.upload_to_info = (path, name) 
    file = ImageField(max_length=500, upload_to=user_directory_path) 
    def filename(self): 
     return os.path.basename(self.file.name) 
    theplace = models.ForeignKey(ThePlace, null=True, blank=True, related_name='pictures') 
    def __unicode__(self): 
     return str(self.id) 
    def save(self, *args, **kwargs): 
     super(Picture, self).save(*args, **kwargs) 

私はフォームに問題なくエントリを作成しました。なぜ、このフィールドを管理者が完了する必要があるのか​​理解できません。フィールドをモデル化するnull引数に関するdocsから

答えて

1

:あなたは空の許可したい場合は、文字列ベースおよび非文字列ベースのフィールドの両方のために

、あなたはまた、真=空白を設定する が必要になります nullパラメータはデータベースストレージにのみ影響します(blankを参照)。

管理者はフォームの作成と編集にフォームを使用するため、管理者に空欄のままにしたいフィールドにblank=Trueが必要です。

関連する問題