我一直在尝试在我的TrainingSubject模型中添加一个名为" email"并通过外键将其值链接到另一个模型(Training),然后将其全部显示在前端(后面是django,前面是反应。
我已将此行添加到 TrainingSubject 模型中:
email = models.CharField(_('Email assigned'), max_length=100, default='', blank=True)
这个到培训模型:
email_to = models.ForeignKey(TrainingSubject, related_name='manager_email', default='', blank=True)
所以全部内容如下:
class TrainingSubject(models.Model):
"""TrainingSubject model for storing subjects."""
name = models.CharField(_('name'), max_length=160, unique=True)
email = models.CharField(_('Email assigned'), max_length=100, default='', blank=True)
def __str__(self):
return self.name
class Meta:
verbose_name = _('Training subject')
verbose_name_plural = _('Training subjects')
class Training(models.Model):
"""Training model for storing trainings."""
subject = models.ForeignKey(TrainingSubject, verbose_name=_('subject'),
related_name='trainings')
email_to = models.ForeignKey(TrainingSubject, related_name='manager_email', default='', blank=True)
date = models.DateTimeField(_('date'))
timezone = models.CharField(
无法应用迁移,每次我开始创建这些内容时都会收到此错误。
Running migrations:
Rendering model states... DONE
Applying sonel_core.0089_training_email_to...Traceback (most recent call last):
File "/home/vagrant/.pyenv/versions/cms/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "email_to_id" contains null values
以前我只尝试将其全部添加到一步,现在我将其分解为两个步骤:1。将电子邮件添加到TrainingSubject(效果非常好)和2.添加 email_to 到培训模型,并能够最终在前端显示此电子邮件。
我做错了什么?
答案 0 :(得分:0)
定义字段以接受空值,因为表已经有一些数据,在添加新的FK字段时,必须给出null或有效值。
email_to = models.ForeignKey(TrainingSubject, related_name='manager_email', default='', blank=True, null=True)