【问题标题】:models.E005 The field 'xxx' from parent model 'x.x' clashes with the field 'xxx' from parent modelmodels.E005 父模型“x.x”中的字段“xxx”与父模型中的字段“xxx”冲突
【发布时间】:2021-10-19 14:22:49
【问题描述】:

Django 继承错误:(models.E005)

你有解决这个问题的方法吗,有没有办法添加前缀或 idk,因为我必须有几个具有相同遗产的用户?

Django 不喜欢这样:

类 Dispositif(Infos, ChefGO, ADJChefGO):

bcs ChefGO 和 ADJChefGO 依赖于同一个类成员,但我确实需要两个成员

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)


谢谢

【问题讨论】:

    标签: django inheritance model


    【解决方案1】:

    外键是一种解决方法吗?

        # model
        class Dispositif(Infos):
            myChefGO = models.ForeignKey(ChefGO, on_delete=models.CASCADE)
            myADJChefGO = models.ForeignKey(ADJChefGO, on_delete=models.CASCADE)
    
        # instanciation
        dispositif = Dispositif(Infos)
        dispositif.myChefGO = A
        dispositif.myADJChefGO = B
    

    【讨论】:

      【解决方案2】:

      我已经找到了这个方法的解决方案:

      1- 使用“字段”和“表单”创建“设备”类

      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- 像这样使用“设备”字段

      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- 继承

      class Dispositif(ChefGO, ADJChefGO):
          pass
      

      4-“奖金” 您可以像这样在 ModelForm 中使用它:

      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()
              }
      

      【讨论】:

        猜你喜欢
        • 2016-03-09
        • 2021-11-29
        • 1970-01-01
        • 2022-10-24
        • 2013-11-09
        • 1970-01-01
        • 2016-08-17
        • 2013-10-18
        • 2016-06-26
        相关资源
        最近更新 更多