【问题标题】:How to refer to self (model) in mongoengine document definition如何在 mongoengine 文档定义中引用自身(模型)
【发布时间】:2018-10-05 10:26:44
【问题描述】:

我想根据我数据库中的项目数设置uuid。

class Order(Document):
    uuid = None

    def __init__(self):
        self.uuid = self.gen_uuid()

    def gen_uuid(self):
        date = datetime.now().strftime('%d%m%y')
        count = self.objects.count()
        return date + str(count)

不幸的是,引用self.objects.count() 抛出AttributeError: 'QuerySetManager' object has no attribute 'count。

我很困惑,因为这是访问模型数据的 API 方式。因此,我的问题是,在模型定义中引用模型数据的正确方法是什么?

【问题讨论】:

    标签: python mongoengine


    【解决方案1】:

    解决方案

    用model name 替换self 解决了我的问题。

    # from 
    count = self.objects.count()
    
    # to 
    count = Order.objects.count()
    

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      相关资源
      最近更新 更多