0
我AbstractProfile
模型預定義PRIVACY_CHOICES
:如何覆蓋抽象類的選擇?
class AbstractProfile(models.Model):
PRIVACY_CHOICES = (
(1, _('all')),
(2, _('no one')),
)
title = models.CharField(_('title'), max_length=30)
info = models.TextField(_('information'), max_length=500, blank=True)
info_privacy = models.IntegerField(_('show information to'), default=1, choices=PRIVACY_CHOICES)
city = models.CharField(_('location'), max_length=30, blank=True)
city_privacy = models.IntegerField(_('show location to'), default=1, choices=PRIVACY_CHOICES)
address = models.CharField(_('address'), max_length=30, blank=True)
address_privacy = models.IntegerField(_('show address to'), default=1, choices=PRIVACY_CHOICES)
class Meta:
abstract = True
class UserProfile(AbstractProfile):
PRIVACY_CHOICES = (
(1, _('all')),
(2, _('friends')),
(3, _('friends of friends')),
(4, _('only me')),
)
title = None
first_name = models.CharField(_('first name'), max_length=30, blank=True)
last_name = models.CharField(_('last name'), max_length=30, blank=True)
names_privacy = models.IntegerField(_('show names to'), default=1, choices=PRIVACY_CHOICES)
birth_date = models.DateField(_('birth date'), null=True, blank=True)
birth_date_privacy = models.IntegerField(_('show birth date to'), default=1, choices=PRIVACY_CHOICES)
avatar = models.ImageField(upload_to='users/avatar', null=True, blank=True)
UserProfile
應該從場AbstractProfile
,但有自己的PRIVACY_CHOICES
。在當前實現PRIVACY_CHOICES
的UserProfile
不覆蓋PRIVACY_CHOICES
的AbstractProfile
。如何解決?在未來可能是其他機型,其中也應該有自己的PRIVACY_CHOICES
我使用Django 1.10
不是在抽象中定義隱私字段,而是爲什麼不把它移動到CompanyProfile而不是在抽象中定義它? –
你的意思是根據需要在許多課程中重複相同的字段?不是乾的方式:) – TitanFighter
我看到兩個模型和兩個定義。看起來不像我。而且,DRY是重要的,但通常運營解決方案更重要。我想這是最好的另一個場地的討論。 –