表单引用外键字段并进行加工处理

这是第一个表单
图片描述

其中字段 用户编码 的定义

class CustomerAccount(models.Model):
    xxxxxxx
 @property
    def customer_no(self):
          xxxxxxx
        return '%s%s%s%s' % (
        source, role, self.register_date.strftime('%Y%m%d'), no)

第二个表单方案管理里字段 方案编码规则为
:方案编码=用户编码+三位数字

class Macro(models.Model):

    account = models.ForeignKey('customers.CustomerAccount',
                                related_name='macros',
                                default=None, null=True, blank=True,
                                on_delete=models.SET_NULL)
                                
                                
                                    @property
    def style_no(self):
        no = self.no
        for i in range(3 - len('%s' % self.no)):
            no = '0%s' % no
        return '%s%s' %(CustomerAccount.customer_no, no)

图片描述
引用第一个表单里的class 属性customer_no,结果如下:
图片描述

返回的是属性对象,该如何处理?

阅读 2.5k
1 个回答

customer_no 是实例属性,要通过实例进行调用,我猜你是想:

return '%s%s' %(self.account.customer_no, no)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题