如何从makemigrations命令添加另一个0000n_initial.py文件

时间:2019-11-06 02:05:05

标签: django

我想从makemigrations文件中添加另一个initial.py文件

当我尝试py manage.py makemigrations时显示错误

  
    

您正在尝试向用户添加不可为空的字段“ role”,而没有默认设置;我们无法做到这一点(数据库需要一些东西来填充现有行)。

         

请选择一个修复程序:

         
        
  1. 现在提供一个一次性的默认值(将在所有现有行中设置为该列的空值)

  2.     
  3. 退出,然后让我在models.py

  4. 中添加默认值     
  

我想通过另一个init进行依赖下拉列表

这是我在models.py中写的

import datetime

from django.db import models
from django.utils import timezone

class TableAll(models.Model):
    table_name = models.CharField(max_length=250)

这是0001_initial.py的代码

# Generated by Django 2.2.2 on 2019-10-29 03:52

from django.db import migrations, models
from django.contrib.auth.models import User
import django.db.models.deletion

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    role = models.CharField(max_length=250)
    role.contribute_to_class(User,'role')    

    operations = [

        migrations.CreateModel(
            name='TableAll',
            fields=[                
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('table_name', models.CharField(max_length=250)),                
            ],
        ),

    ]



我想向另一个模型添加外键,并想进行迁移以添加新的00002_initial.py(例如),并且我想要这样的东西(这是我从github源码中找到的代码)

新的初始文件(0002_add_initial_data.py)//这只是github源代码的示例,其中具有0001_initial.py和0002_add_initial_data.py来使依赖的下拉列表起作用


class Migration(migrations.Migration):
    Country = apps.get_model('hr', 'Country')
    City = apps.get_model('hr', 'City')

    india = Country.objects.create(name='India')
    City.objects.create(name='Bengaluru', country=india)
    City.objects.create(name='Mumbai', country=india)
    City.objects.create(name='Chennai', country=india)
    City.objects.create(name='Hyderabad', country=india)
    City.objects.create(name='New Delhi', country=india)

    usa = Country.objects.create(name='United States')
    City.objects.create(name='New York', country=usa)
    City.objects.create(name='San Francisco', country=usa)
    City.objects.create(name='Los Angeles', country=usa)
    City.objects.create(name='Chicago', country=usa)
    City.objects.create(name='Seattle', country=usa)

    russia = Country.objects.create(name='Russia')
    City.objects.create(name='Moscow', country=russia)
    City.objects.create(name='Saint Petersburg', country=russia)
    City.objects.create(name='Yekaterinburg', country=russia)
    City.objects.create(name='Kazan', country=russia)
    City.objects.create(name='Krasnodar', country=russia)

    brazil = Country.objects.create(name='Brazil')
    City.objects.create(name='Sao Paulo', country=brazil)
    City.objects.create(name='Rio de Janeiro', country=brazil)
    City.objects.create(name='Belo Horizonte', country=brazil)
    City.objects.create(name='Curitiba', country=brazil)
    City.objects.create(name='Recife', country=brazil)

    uk = Country.objects.create(name='United Kingdom')
    City.objects.create(name='London', country=uk)
    City.objects.create(name='Huddersfield', country=uk)
    City.objects.create(name='Glasgow', country=uk)
    City.objects.create(name='Edinburgh', country=uk)
    City.objects.create(name='Cambridge', country=uk)

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

    operations = [
        migrations.RunPython(add_initial_data),
    ]

感谢您的帮助,这是django框架的新功能,我需要在论文中使用它。.

1 个答案:

答案 0 :(得分:1)

将非空字段添加到模型中并且该字段的值是脚本的结果时,可以遵循一种模式。

  1. 将字段添加到模型中,但使其可为空(null = True)
  2. 运行makemigrations以创建添加可为空字段的迁移
  3. 创建另一个使用migrations.RunPython填充此列的迁移(makemigrations --empty)
  4. 从字段中删除null = True,然后再次运行makemigrations。提示选择时,选择1“立即提供一个默认值”,然后输入所需的整数,因为所有行都应该已经有值了
  5. 迁移