Django、South 和 --freeze 命令

发布于 2024-11-11 15:28:40 字数 1950 浏览 3 评论 0原文

我正在尝试开始南方数据迁移。我发现了这个问题:南方数据迁移“实例”使用 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 选项应该使 app_A 的模型在通过 orm['app_1.'] 的迁移中可用。然后我编辑了这样创建的迁移:

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'

我做错了什么吗?

预先感谢您的帮助!

I'm trying to get started with South data migrations. I found this SO question: South data migration 'instance' error when using south freeze orm, and tried the commands listed there but it does not seem to help.

I have 2 apps A and B in my project, here are their models:

# /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)

I am trying to generate a data migration for app_B so that it calculates the age and senority of the Employee the Task is assigned to and store it in the Task itself. I ran :

./manage.py datamigration app_B populate_age_and_senority --freeze app_A

the --freeze option should make the models of app_A available in the migration through orm['app_1.']. I then edited the migration created this way:

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.")

And then ran:

./manage.py migrate app_B

Here is what I obtained:

AttributeError: type object 'Employee' has no attribute 'birthdate'

Did I do something wrong?

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-11-18 15:28:41

该行:

orm['hr_manager.Employee']

访问模型,而不是实例。您需要通过通常的:

orm['hr_manager.Employee'].objects.all()
orm['hr_manager.Employee'].objects.get(...)
...

方法访问实例。

The line:

orm['hr_manager.Employee']

Accesses the Model, not an instance. You need to access an instance via the usual:

orm['hr_manager.Employee'].objects.all()
orm['hr_manager.Employee'].objects.get(...)
...

methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文