假设我有这些模型和字段:
class User(AbstractBaseUser):
name_title
name_first
name_middle_initial
name_last
address_1
address_2
address_city
address_state
address_post_code
class Order(models.Model):
name_title
name_first
name_middle_initial
name_last
address_1
address_2
address_city
address_state
address_post_code
class Shipment(models.Model):
address_1
address_2
address_city
address_state
address_post_code
假设这些模型中没有一个是必然相关的——订单不一定属于用户,发货不一定属于订单,等等。
我希望所有重复字段都相同——具有相同的详细名称、验证、最大长度等。
我尝试过采用混合方法:
class AddressFieldsMixin(models.Model):
address_1
address_2
address_city
address_state
address_post_code
class NameFieldsMixin(models.Model):
name_title
name_first
name_middle_initial
name_last
class User(AbstractBaseUser, AddressFieldsMixin, NameFieldsMixin):
pass
class Order(models.Model, AddressFieldsMixin, NameFieldsMixin):
pass
class Shipment(models.Model, AddressFieldsMixin):
pass
Mixin...但是如果我的类继承自,这会导致模型/继承冲突,如果不继承,则会导致models.Model“未知字段”错误。
在多个模型中重复使用“名称”字段和“地址”字段的正确方法是什么?谢谢!
aluckdog
相关分类