(ちょうどsqliteの切り替えと前にこの問題を持っていなかった)ジャンゴOneToOneField
だから私は3つのモデルがあります:
モデル。 PY
class Person(models.Model):
is_parent = models.BooleanField()
class VideoGamePurchase(models.Model):
bought_by = models.ForeignKey(Person)
after_homework = models.OneToOneField(HomeWork, OPTIONS???)
class HomeWork(models.Model):
done_by = models.ForeignKey(Person)
content = models.CharField(blablaba)
だから私が実装しようとしてるのロジックはPerson.is_parent
がTrue
VideoGamePurchase
インスタンスのことが可能であるならばということですafter_homework
の空または空フィールドで作成する必要があります。ただし、Person.is_parent
がFalse
の場合は、このフィールドを一意のHomeWork
オブジェクトの主キーにします。
私はこれを達成するために、右のオプションを見つけることができません。
私はprimary_key=True
を持っていない場合は、makemigrationsが失敗した:
You are trying to add a non-nullable field 'id' to video_game_purchase without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
ので、私は私が
primary_key=True
を持っている肝炎と思います。しかし、私は
null=True
または
blank=True
を持つことができないようです。
OneToOneField
をpostgreSQLで空にする方法はありますか?
このようなロジックを実装する他の方法がありますか?
ありがとうございました!
Nailed it! ありがとう! –