Hello, I have the task of transferring a project from django version 1.8.2 to version 1.11. Faced a problem with which I can not cope. The table model, which worked perfectly in 1.8.2, stopped working in version 1.11, and crashes with the error:

core.CarModel.manufacture: (models.E006) The field 'manufacture' clashes with the field 'manufacture' from model 'core.page'. 

Apparently this is due to the inheritance of models, since the old project I can not drastically change the table schema, please tell me how to get out of this situation. Thanks in advance for your help.

I reproduced the model diagram, which works fine on 1.8.2 and stops working on 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): manufacture = models.ForeignKey(Manufacture, verbose_name=u'Manufacture') ru_title = models.CharField(max_length=64, verbose_name=u'RU Title') 

    2 answers 2

    This is not a jungle error, or a version incompatibility. You have the same name.

    The fact is that the Page models, you need the name of the manufacture, to access the Manufacture model, and CarModel has a manufacture field, which also needs to be displayed. Naturally, she cannot identify two identical names.

    One solution is to rename the manufacture field at CarModel, or the model name Manufacture, so that the names do not match.

    • Thank you very much for the tip - Dmitriy Yusupov
    • Is this a tip? You have one-to-one relationship between models there. - Mr Fix
    • Do you mean that you can mark the issue as resolved? Probably you are right, but I still hope that maybe there is some way to note, say in the model (some instruction), that refer to the field by some name other than the gene, so as not to change the names of the tables or fields in the database table. Perhaps there is no such method, but in 1.8 it somehow works. At least there is a way that you have proposed, for which many thanks, at least you clarified the essence of the error. - Dmitriy Yusupov
    • This code could not work in any version. It could work only if the Page model was abstract. - Mr Fix
    • Why should I lie, put in virtualenv 1.8.2 and make sure - Dmitriy Yusupov

    Unfortunately, no, the functionality that you are waiting for is not returned. Dmitriy Yusupov said everything correctly - you only have the option to rename the field or model (call it CarManufacture, what's the problem).

    This behavior really worked in 1.8.x, because the parent in the multi table inheritance would be called manufacture_ptr, and in django 1.9, this functionality would be changed and equated to the model name. Alas, he came across this, did not find a clear solution.

    There is another option to pass the name parameter to the field, but this is not what you expect from the jungle, it just renames it in the calls, confusing even more

     manufacture = models.ForeignKey(Manufacture, verbose_name=u'Manufacture', name='manufacture_ptr') 

    calls change: CarModel.objects.create(manufacture_ptr=Manufacture.objects.first())