将类方法改编为另一个类

时间:2017-03-19 18:20:28

标签: python django

 @python_2_unicode_compatible
 class EmployerProfile(AbstractAddress):
      customer = models.OneToOneField(
          CustomerProfile, verbose_name=_('Customer'),
          related_name='employerprofile')
      company_name = models.CharField(_('Company name'),
                                        max_length=50, blank=True, null=True)
            ...

    #That function will empty all fields in this model whenever income_source
    # is different of 'Employed'
    def empty_fields(self):
        if self.income_source != 'Employed':
            to_empty = [f.name for f in EmployerProfile._meta.get_fields()]
            exclude = [u'id', "has_missing_fields",
                       "manual_validation", "actor_actions",
                       "target_actions", "action_object_actions",
                       ]
        for field_name in to_empty:
            if field_name not in exclude:
                setattr(self, field_name, None)

@python_2_unicode_compatible
class FinancialProfile(models.Model):
    customer = models.OneToOneField(
        CustomerProfile, verbose_name=_('Customer'),
        related_name='financialprofile')
    income_source = models.CharField(_('Income source'), max_length=20,
                                     choices=settings.LOANWOLF_INCOME_SOURCE_CHOICES,
                                     default='employed')

我想在课程empty_field()中移动FinancialProfile。实际上,只要income_source(FinancialProfile)!='Employed',此方法必须从EmployerProfile清空属性。谁能告诉我怎么能这样做呢?

P.S。请告诉我是否有不清楚的事情。

谢谢!

0 个答案:

没有答案