繁体   English   中英

Django ORM呼叫管理器方法来自另一个管理器?

[英]Django ORM call manager method from another manager?

关于如何从Manager.py中访问其他管理器方法的任何提示?

不管我做什么,我似乎都无法访问其他经理的方法。 Python抱怨它没有定义...

如果我在manager.py中导入模型,会引起问题吗? 通函包括什么?

manager.py:

# Returns the whole family who are active
def get_active_dependents_including_guardian( self, consumer, connectedOnly = False ):
    logger.debug('get_active_dependents_including_guardian')
    results = self.model.objects.filter( guardian = consumer,
                                      is_active = True ).order_by('dob')

    if connectedOnly:
        from myir import models
        #OPTIMIZE: this can be optimized if I query for all patient ids for each dependent in one trip.  But I don't even know how to do this yet cause I'm a noob.
        results = [d for d in results if models.DependentPatientID.objects.get_patient_ids(d)[0].patient_id_integer == 0] **#HERE IS PROBLEM**

    return results

# some stuff omitted...
# this is the manager of models.DependentPatientId
class DependentPatientIDManager( models.Manager ):
    def get_patient_ids(self, dependent ):
        dpid = self.model.objects.get( dependent = dependent.id )
        return dpid 

您需要更改:

from myir import models

from myir.models import DependentPatientID

原因是,您可能已经from django.db import models完成操作from django.db import models并且名称冲突。

现在,

class DependentPatientIDManager( models.Manager ):
    def get_patient_ids(self, dependent ):
        dpid = self.model.objects.get( dependent = dependent.id )
        return dpid 

返回一个对象,而不是一个查询集。 因此, DependentPatientID.objects.get_patient_ids(d)[0]将失败。

所以试试这个

if connectedOnly:
    from myir.models import DependentPatientID
    patient_id_integer = 0
    dep_patient_id = DependentPatientID.objects.get_patient_ids(d) 
    if dep_patient_id:
        patient_id_integer = dep_patient_id.patient_id_integer
        results = [d for d in results if patient_id_integer == 0]

        #Or just

        if not patient_id_integer:
            results = []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM