是否应该将仅限管理员的字段提取到与原始模型具有 OneToOne 关系的专用模型中?

should admin-only fields be extracted into a dedicated model which has a OneToOne relation to the original model?

我有以下型号

class User(AbstractUser):
    # other fields...
    billing_active = models.BooleanField(
        _('Billing active'),
        default=True,
        help_text=_(
            'designates whether the user should be charged'
        )
    )
    billing_start = models.DateField(_('Billing cycle start'))
    billing_end = models.DateField(_('Billing cycle end'))
    billing_charge = models.DecimalField(
        _('Charge'),
        max_digits=5,
        decimal_places=2,
        help_text='amount to be charged'
    )

通过查看那些前缀 billing_*,字段似乎应该属于一个新对象,这似乎没问题,因为它只是一个数据库表示。但是 billing_activebilling_charge 是管理字段,用户不能更改,创建一个包含所有这些字段的新模型 UserBillingSettings 会不会很好?然后我可以使用 django 的内置权限系统:

class User(AbstractUser):
    # fields ...

class UserBillingSettings(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='billing_settings')
    # billing fields
    class Meta:
        permissions = (('can_change', 'Change user billing settings'),)

你完全正确,你总是需要将这些细节-与用户table没有直接关系的细节分开,放到他们自己的table中。

class BillingInfo(models.Model):
    billing_active = models.BooleanField(
        _('Billing active'),
        default=True,
        help_text=_(
            'designates whether the user should be charged'
        )
    )
    billing_start = models.DateField(_('Billing cycle start'))
    billing_end = models.DateField(_('Billing cycle end'))
    billing_charge = models.DecimalField(
        _('Charge'),
        max_digits=5,
        decimal_places=2,
        help_text='amount to be charged'
    )
    ...
    class Meta:
        permissions = (('can_change', 'Change user billing settings'),)

class User(AbstractUser):
    ... user info
    billing_info = models.OneToOneField(BillingInfo, on_delete=models.CASCADE)