models.E005 The field 'xxx' from parent model 'x.x' clashes with the field 'xxx' from parent model

Issue

Django inheritance error: (models.E005)

Do you have a solution to get around this problem, is there a way to add a prefix or idk because I must have several User with the same heritage?

Django dont like this:

class Dispositif(Infos, ChefGO, ADJChefGO):

bcs ChefGO and ADJChefGO depend on the same class Member, but i realy need two member

class Members(models.Model):
    phone_regex = RegexValidator(regex=r'^[0-9]{10}$', message="Error: Format 0611223344")
    indicative = models.CharField(max_length=16, default="Default indicative")
    phone = models.CharField(max_length=10, validators=[phone_regex])

    class Meta:
        abstract = True


class ChefGO(Members):
    pass


class ADJChefGO(Members):
    pass


class Dispositif(Infos, ChefGO, ADJChefGO):
    name = models.CharField(max_length=32, default="Name")
    place = models.CharField(max_length=64)
    start_date = models.DateTimeField(default=datetime.datetime.now)
    end_date = models.DateTimeField(default=datetime.datetime.now)


Thanks

Solution

I have found solution with this method:

1- Create "Device" class with ‘Fields’ and ‘Forms’

class Device:
    @staticmethod
    class Fields:

        @staticmethod
        class Member:
            @staticmethod
            def indicative():
                return models.CharField(max_length=16, default="Default indicative")

            @staticmethod
            def phone():
                phone_regex = RegexValidator(regex=r'^[0-9]{10}$', message="Error: Format 0611223344")
                return models.CharField(max_length=10, validators=[phone_regex])

            @staticmethod
            def function():
                return models.CharField(max_length=10)

    @staticmethod
    class Forms:

        @staticmethod
        class Member:
            @staticmethod
            def indicative():
                return django.forms.TextInput(attrs={'placeholder': 'indivatif'})

            @staticmethod
            def phone(placeholder="06 12 34 56 78"):
                return django.forms.TextInput(attrs={'placeholder': placeholder})

            @staticmethod
            def function():
                return django.forms.TextInput(attrs={'placeholder': 'fonction'})

2- Use ‘Device’ Fields like this

class ChefGO(models.Model):
    cgo_indicative = Device.Fields.Member.indicative()
    cgo_phone = Device.Fields.Member.phone()
    cgo_function = Device.Fields.Member.function()

    class Meta:
        abstract = True


class ADJChefGO(models.Model):
    adjcgo_indicative = Device.Fields.Member.indicative()
    adjcgo_phone = Device.Fields.Member.phone()
    adjcgo_function = Device.Fields.Member.function()

    class Meta:
        abstract = True

3- Inheritance

class Dispositif(ChefGO, ADJChefGO):
    pass

4- "Bonus"
You can use’it in ModelForm like that:

class DispositifForm(ModelForm):
    class Meta:
        model = Dispositif
        
        fields = [
                    'cgo_phone', 'cgo_indicative', 'cgo_function',
                    'adjcgo_phone', 'adjcgo_indicative', 'adjcgo_function',
                    'dso_phone', 'dso_indicative', 'dso_function'
                ]
        
        widgets = {
            'cgo_phone': Device.Forms.Member.phone(),
            'cgo_indicative': Device.Forms.Member.indicative(),
            'cgo_function': Device.Forms.Member.function(),
            
            'adjcgo_phone': Device.Forms.Member.phone(),
            'adjcgo_indicative': Device.Forms.Member.indicative(),
            'adjcgo_function': Device.Forms.Member.function(),
            
            'dso_phone': Device.Forms.Member.phone(),
            'dso_indicative': Device.Forms.Member.indicative(),
            'dso_function': Device.Forms.Member.function()
        }

Answered By – gabriel

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published