简体   繁体   English

Django:将新外键添加到现有模型中,并将默认值作为同一模型中的另一个外键

[英]Django: Add new foreign key to existing model with default value as another foreign key from the same model

I recently started using Django, so please be patient. 我最近开始使用Django,因此请耐心等待。 I have a model with 2 foreign keys 我有一个带有2个外键的模型

class Application(models.Model): assessment_owner = models.ForeignKey(User, related_name='assessment_owner') creator = models.ForeignKey(User, related_name='creator')

I am trying to add new foreign key called tech_lead to the same model, and default value for tech_lead should be assessment_owner. 我正在尝试将名为tech_lead的新外键添加到同一模型中,并且tech_lead的默认值应为Assessment_owner。 Later on, I can update the value for tech_lead using data load, but initially it should be assessment owner. 稍后,我可以使用数据加载来更新tech_lead的值,但最初它应该是评估所有者。

With following code snippet, Django asks for a default value while making migrations and assigns the same tech_lead everywhere. 在以下代码段中,Django在进行迁移时要求提供默认值,并在各处分配相同的tech_lead。 I would like to define default value for tech_lead through code, and simple default attribute doesn't work. 我想通过代码为tech_lead定义默认值,并且简单的默认属性不起作用。 I have tried using signals pre_save and post_save with no luck. 我试过使用信号pre_save和post_save没有运气。

class Application(models.Model):
    assessment_owner = models.ForeignKey(User, related_name='assessment_owner')
    creator = models.ForeignKey(User, related_name='creator')
    tech_lead = models.ForeignKey(User, related_name='tech_lead')

I am using Django 1.11.3 and postgreSQL. 我正在使用Django 1.11.3和postgreSQL。

Migration was successful with one-off default value. 一次性成功使用默认值进行迁移。

Error Stack - 错误堆栈-

Env details 环保详情

error 错误

error 错误

Thanks in advance. 提前致谢。

tech_lead = models.ForeignKey(User, related_name='tech_lead')

breaks integrity because your database is already populated with Application instances. 破坏完整性,因为您的数据库已经装有Application实例。 If you want to add a not nullable FK to your scheme, you should specify default value. 如果要向方案添加不可为空的FK,则应指定默认值。 Otherwise, if you can't provide default value, you should consider allowing tech_lead to be NULL, ie: 否则,如果您不能提供默认值,则应考虑允许tech_lead为NULL,即:

tech_lead = models.ForeignKey(User, related_name='tech_lead', null=True)

then using data migration to populate field with values you want: 然后使用数据迁移用所需的值填充字段:

from django.db import migrations

def populate_tech_lead(apps, schema_editor):
    Application = apps.get_model('yourappname', 'Application')
    for application in Application.objects.all():
        application.tech_lead = application.assessment_owner
        application.save()

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(populate_tech_lead),
    ]

and then removing null=True from the field: 然后从字段中删除null=True

tech_lead = models.ForeignKey(User, related_name='tech_lead')

Step 1. add null=True to the tech_lead field as 步骤1.将null=True添加为tech_lead字段,如下所示:

class Application(models.Model):
    assessment_owner = models.ForeignKey(User, related_name='assessment_owner')
    creator = models.ForeignKey(User, related_name='creator')
    tech_lead = models.ForeignKey(User, related_name='tech_lead', null=True)

Step 2. create migration file by python manage.py makemigrations 步骤2.通过python manage.py makemigrations创建迁移文件

Step 3. migrate the db python manage.py migrate 步骤3.迁移数据库python manage.py migrate

Step 4. open django shell, python manage.py shell 步骤4.打开Django shell, python manage.py shell

Step 5. run the following script 步骤5.运行以下脚本

from your_app.models import Application
from django.db.models.expressions import F

Application.objects.filter(tech_lead__isnull=True).update(tech_lead=F('assessment_owner'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM