我有这两个类:
class Bill(models.Model):
date = models.DateField()
total_amount_chf = models.DecimalField('Cost (in CHF)', max_digits=10, decimal_places=2)
class ProjectParticipation(models.Model):
project = models.ForeignKey('Project')
user = models.ForeignKey(User)
is_admin = models.BooleanField()
bill = models.OneToOneField(Bill, on_delete=models.SET_NULL, null=True, blank=True)
当我现在构建 SQL 数据库时,我在 ProjectParticipation 的表中得到以下字段:
bill_id integer NOT NULL,
CONSTRAINT expenses_projectparticipation_bill_id_fkey FOREIGN KEY (bill_id)
REFERENCES expenses_bill (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
现在,当我想在没有 Bill 的情况下插入 ProjectParticipation 时,我得到一个““bill_id”列中的空值违反了非空约束”。
该怎么做呢?