2014-09-24 98 views
0

我想升級到Django 1.7並從南切換到Django的集成遷移。我遇到了模型問題。我有一個基類:Django 1.7遷移與抽象基類

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遷移可以處理抽象基類嗎?我寧願保留我的抽象基類並繼承它,但是,將必要的函數複製並粘貼到適當的類中是最後的手段。

回答

2

你的抽象基類應該擴展models.Model

class CalendarDisplay(models.Model): 
    ... 

你的孩子類應該那麼只有延長CalendarDisplay - 它並不需要延長models.Model

class Reservation(CalendarDisplay): 
    ... 

此外,你需要或者不要在CalendarDisplay中覆蓋__init__或者您需要在您的init中調用super

def __init__(self, *args, **kwargs): 
    super(CalendarDisplay, self).__init__(*args, **kwargs)