我正在尝试升级到Django 1.7并从南方切换到Django的集成迁移。我的模特遇到了问题。我有一个基类:
class CalendarDisplay():
"""
Inherit from this class to express that a class type is able to be displayed in the calendar.
Calling get_visual_end() will artificially lengthen the end time so the event is large enough to
be visible and clickable.
"""
start = None
end = None
def __init__(self):
pass
def get_visual_end(self):
if self.end is None:
return max(self.start + timedelta(minutes=15), timezone.now())
else:
return max(self.start + timedelta(minutes=15), self.end)
class Meta:
abstract = True
以下是继承它的类的示例:
class Reservation(models.Model, CalendarDisplay):
user = models.ForeignKey(User)
start = models.DateTimeField('start')
end = models.DateTimeField('end')
...
现在我尝试迁移我的应用,但收到以下错误:python manage.py makemigrations
Migrations for 'Example':
0001_initial.py:
- Create model Account
...
ValueError: Cannot serialize: <class Example.models.CalendarDisplay at 0x2f01ce8>
There are some values Django cannot serialize into migration files.
我有什么选择解决这个问题? Django 1.7迁移可以处理抽象基类吗?我宁愿保留我的抽象基类并继承它,但是,将必要的函数复制并粘贴到适当的类中是最后的手段。
答案 0 :(得分:2)
您的抽象基类应该扩展models.Model
:
class CalendarDisplay(models.Model):
...
您的子课程应该只延长CalendarDisplay
- 它不需要延长models.Model
:
class Reservation(CalendarDisplay):
...
此外,您需要覆盖__init__
中的CalendarDisplay
或者您需要在初始化中调用super
:
def __init__(self, *args, **kwargs):
super(CalendarDisplay, self).__init__(*args, **kwargs)