将模型文件拆分成多个单独的模型会产生字段抽象错误

时间:2018-05-25 08:52:39

标签: django django-models

我的代码如下所示:

holidayRequest.py

from django.db import models
from enum import Enum

class HolidayRequests(models.Model):

    class Meta:
        db_table = 'holidays_requests'
        app_label = 'dashboard'

    name = models.CharField(max_length = 255,null= False)
    email = models.CharField(max_length = 255,null=False)
    ph_no = models.CharField(max_length =255,null=False)


    holiday = models.ForeignKey('holidays',on_delete=models.CASCADE)

holiday.py

from django.db import models
from enum import Enum

class Holiday (models.Model):

    class Meta:
        db_table = 'holidays'
        app_label = 'dashboard'

    holiday_price  = models.CharField(max_length = 255,null =False)
    holiday_title = models.CharField(max_length=255,null=False)

init.py (在模特文件夹内)

from holidayRequest import HolidayRequests
from holiday import Holiday

当我运行迁移时,我收到以下错误:

dashboard.HolidayRequests.holiday: (fields.E300) Field defines a relation with model 'holidays', which is either not installed, or is abstract.
dashboard.HolidayRequests.holiday: (fields.E307) The field dashboard.HolidayRequests.holiday was declared with a lazy reference to 'dashboard.holidays', but app 'dashboard' doesn't provide model 'holidays'.

1 个答案:

答案 0 :(得分:3)

您必须指定型号名称,而不是db_table_name

holiday = models.ForeignKey('Holiday',on_delete=models.CASCADE)

另外,从__init__.py删除导入。