Django migrations / South:新列从同一记录中获取另一个值的默认值

时间:2014-09-01 15:33:34

标签: sql django migration django-south relational

我想在现有表中添加一个新列,但我想根据已有数据给它一个默认值:

e.g。每条记录都有一个start_date。现在我想添加一个open_until列,我希望用每个现有记录的start_date填充它。 (即将到来的记录将能够选择不同的价值)

有友好的方法吗?

3 个答案:

答案 0 :(得分:12)

您也可以在南方进行。唯一需要注意的是,您需要两个步骤:

  1. 添加open_until列的架构迁移

    from django.db import models
    import datetime
    
    class MyModel(models.Model):
        start_date = models.DateField(),
        open_until = models.DateField(default=datetime.date.today),
    

    $ python manage.py schemamigration --auto appname

  2. 数据迁移,用其他列的值填充现有行

    $ python manage.py datamigration appname populate_open_until

    import datetime
    
    class Migration(DataMigration):
    
        def forwards(self, orm):
            "Set open_until value to that of start_date for existing rows"
            for t in orm.MyModel.objects.all():
                t.open_until = t.start_date
                t.save()
    
        def backwards(self, orm):
            "Revert back to default"
            for t in orm.MyModel.objects.all():
                t.open_until = datetime.date.today
                t.save()
    
  3. 可选)在步骤1中,您可以提供临时默认值或将其设为可选,并添加第3步

    1. 架构迁移,它使open_until列成为必需列。

答案 1 :(得分:0)

在Python 3.8中,我首先将字段添加到MyApp模型文件中,如下所示:

from django.db import models
import datetime

class MyModel(models.Model):
    start_date = models.DateField(),
    open_until = models.DateField(default=datetime.date.today),

然后,在运行manage.py makemigrations之后,将以下行添加到创建的新迁移文件中:

def forward(apps, schema_editor):
    my_model_objects = apps.get_model('MyApp', 'MyModel').objects.all()
    for t in my_model_objects:
        t.open_until = t.start_date
        t.save()

def reverse(apps, schema_editor):
    pass

class Migration(migrations.Migration):
    operations = [
        " the base operations is here " ,
        migrations.RunPython(forward, reverse),
    ]

答案 2 :(得分:-2)

正如我回复您所说,如果您正在使用框架,则无需在数据库级别设置动态默认值:)

我认为,最好的方法是在保存记录之前在视图中设置列的值。

models.py

from django.db import models

class MyModel(models.Model):
    start_date = models.DateField(),
    open_until = models.DateField(),

forms.py     来自django.forms import ModelForm

class MyForm(forms.ModelForm):
    model = MyModel

    fields = ('start_date')

课程视图

from django.http import HttpResponse
from django.views.generic import CreateView
from .models import MyModel


MyView(CreateView):
    form_class = MyForm

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            submitted_form = form.save(commit=False)
            submitted_form.open_until = form.cleaned_data["start_date"]
            submitted_form.save()
            # return an HttpResponse here

对于前面的条目,使视图只调用一次,然后遍历所有记录并根据订单列的值保存新列的值。

这样的事情:

from django.http import HttpResponse

def set_open_until_values(request)
    records = MyModel.objects.all()
    for record in records:
        record.open_until = record.start_date
        record.save()
    return HttpResponse("Done!!")