Django 多个字段关联同一外键
fields.E302,303,304 错误
from django.db import models
class ops(models.Model):
name = models.CharField(max_length=30)
class shift(models.Model):
shift_class = models.CharField(max_length=30)
class ops_shift(models.Model):
多个字段对应同一个外键的情况下会报错
原因是指向同一模型的三个外键反向关联名称产生了冲突,shift模型对Day1字段的反向关联名称为ops_shift_set(),
对Day2的反向关联名称也是ops_shift_set(),对Day3的反向关联名称还是ops_shift_set()。
解决方法:给models.ForeignKey()加入related_name参数,定义不同的关联名称。
db_column属性是用来定义在数据库中此字段的名称
"""
ops_name = models.ForeignKey(ops,db_column='ops_name')
Day1 = models.ForeignKey(shift,related_name='ops_shift_Day1',default='',db_column='Day1')
Day2 = models.ForeignKey(shift,related_name='ops_shift_Day2',default='',db_column='Day2')
Day3 = models.ForeignKey(shift,related_name='ops_shift_Day3',default='',db_column='Day3')
Day4 = models.ForeignKey(shift,related_name='ops_shift_Day4',default='',db_column='Day4')
Day5 = models.ForeignKey(shift,related_name='ops_shift_Day5',default='',db_column='Day5')
Day6 = models.ForeignKey(shift,related_name='ops_shift_Day6',default='',db_column='Day6')
Day7 = models.ForeignKey(shift,related_name='ops_shift_Day7',default='',db_column='Day7')
Day8 = models.ForeignKey(shift,related_name='ops_shift_Day8',default='',db_column='Day8')
Day9 = models.ForeignKey(shift,related_name='ops_shift_Day9',default='',db_column='Day9')
Day10 = models.ForeignKey(shift,related_name='ops_shift_Day10',default='',db_column='Day10')
Day11 = models.ForeignKey(shift,related_name='ops_shift_Day11',default='',db_column='Day11')
Day12 = models.ForeignKey(shift,related_name='ops_shift_Day12',default='',db_column='Day12')
Day13 = models.ForeignKey(shift,related_name='ops_shift_Day13',default='',db_column='Day13')
Day14 = models.ForeignKey(shift,related_name='ops_shift_Day14',default='',db_column='Day14')
Day15 = models.ForeignKey(shift,related_name='ops_shift_Day15',default='',db_column='Day15')
Day16 = models.ForeignKey(shift,related_name='ops_shift_Day16',default='',db_column='Day16')
Day17 = models.ForeignKey(shift,related_name='ops_shift_Day17',default='',db_column='Day17')
Day18 = models.ForeignKey(shift,related_name='ops_shift_Day18',default='',db_column='Day18')
Day19 = models.ForeignKey(shift,related_name='ops_shift_Day19',default='',db_column='Day19')
Day20 = models.ForeignKey(shift,related_name='ops_shift_Day20',default='',db_column='Day20')
Day21 = models.ForeignKey(shift,related_name='ops_shift_Day21',default='',db_column='Day21')
Day22 = models.ForeignKey(shift,related_name='ops_shift_Day22',default='',db_column='Day22')
Day23 = models.ForeignKey(shift,related_name='ops_shift_Day23',default='',db_column='Day23')
Day24 = models.ForeignKey(shift,related_name='ops_shift_Day24',default='',db_column='Day24')
Day25 = models.ForeignKey(shift,related_name='ops_shift_Day25',default='',db_column='Day25')
Day26 = models.ForeignKey(shift,related_name='ops_shift_Day26',default='',db_column='Day26')
Day27 = models.ForeignKey(shift,related_name='ops_shift_Day27',default='',db_column='Day27')
Day28 = models.ForeignKey(shift,related_name='ops_shift_Day28',default='',db_column='Day28')
Day29 = models.ForeignKey(shift,related_name='ops_shift_Day29',default='',db_column='Day29')
Day30 = models.ForeignKey(shift,related_name='ops_shift_Day30',default='',db_column='Day30')
Day31 = models.ForeignKey(shift,related_name='ops_shift_Day31',default='',db_column='Day31')
class Meta:
"""
在Django的后台可以显示此处定义的中文名称
"""
verbose_name=u'排班表'
verbose_name_plural = u"排班表"