我正在尝试开始南方数据迁移。我发现了这个问题:South data migration 'instance' error when using south freeze orm,并尝试了那里列出的命令,但它似乎没有帮助。
我的项目中有2个应用程序A和B,以下是他们的模型:
# /app_A/models.py
from django.db import models
class Employee(models.Model):
name = models.CharField(max_length = 100)
department = models.ForeignKey("Department")
manager = models.ForeignKey("self", blank = True, null = True)
birthdate = models.DateField()
# /app_B/models.py
from django.db import models
from hr_manager.models import Employee
class Task(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()
assigned_to = models.ForeignKey(Employee, null=False, blank=False)
seniority = models.IntegerField(default=0)
age = models.IntegerField(default=0)
我正在尝试为app_B生成数据迁移,以便计算分配给任务的员工的年龄和年龄,并将其存储在任务本身中。我跑了:
./manage.py datamigration app_B populate_age_and_senority --freeze app_A
--freeze选项应该通过orm ['app_1。']使迁移中的app_A模型可用。然后我编辑了以这种方式创建的迁移:
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
import datetime
def calculate_age(born):
''' Returns the age from a starting date '''
...
birthdate = orm['hr_manager.Employee'].birthdate
date_joined = orm['hr_manager.EmployeeHistory'].date_joined
orm.Task.age = calculate_age(birthdate)
orm.Task.seniority = calculate_age(date_joined)
orm.Task.save()
def backwards(self, orm):
raise RuntimeError("Cannot reverse this migration.")
然后跑了:
./manage.py migrate app_B
这是我获得的:
AttributeError: type object 'Employee' has no attribute 'birthdate'
我做错了吗?
提前感谢您的帮助!
答案 0 :(得分:2)
该行:
orm['hr_manager.Employee']
访问Model
,而非实例。您需要通过以下方式访问实例:
orm['hr_manager.Employee'].objects.all()
orm['hr_manager.Employee'].objects.get(...)
...
方法