我的任务是将项目从 django 1.8.2 版转换为 1.11 版。遇到了我无法应付的问题。该表模型在 1.8.2 中运行良好,但在 1.11 版本中停止工作,并出现错误:
core.CarModel.manufacture: (models.E006) The field 'manufacture' clashes with the field 'manufacture' from model 'core.page'.
显然这是由于模型的继承造成的,由于项目是旧的我无法从根本上改变表格的方案,请告诉我如何摆脱这种情况。预先感谢您的帮助。
我已经复制了模型方案,它在 1.8.2 上运行良好,在 1.11 上停止工作:
# -*- coding:utf-8 -*-
from django.db import models
from django.contrib.contenttypes.models import ContentType
class InheritanceCastModel(models.Model):
real_type = models.ForeignKey(ContentType, editable=False)
def save(self, *args, **kwargs):
if not self.id:
self.real_type = self._get_real_type()
super(InheritanceCastModel, self).save(*args, **kwargs)
def _get_real_type(self):
return ContentType.objects.get_for_model(type(self))
def cast(self):
return self.real_type.get_object_for_this_type(pk=self.pk)
class Meta:
abstract = True
class Page(InheritanceCastModel):
title = models.CharField(max_length=512, verbose_name=u'Title', blank=True)
class Manufacture(Page):
ru_title = models.CharField(max_length=128, verbose_name=u'RU Title')
class CarModel(Page):
# here problem:
manufacture = models.ForeignKey(Manufacture, verbose_name=u'Manufacture')
ru_title = models.CharField(max_length=64, verbose_name=u'RU Title')
因为Page模型是由Manufacture模型继承的,django为Page对象创建了一个代理字段“manufacture”,但是这个名字用在CarModel模型中,导致冲突。在 django 1.8 版本中,该字段被称为“manufacture_ptr”,因此没有冲突。在 django 1.9、1.10、1.11 版本中,该字段被称为“制造”。
解决方法是将 CarModel 模型中的字段名称从“制造”更改为另一个。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句