简体   繁体   中英

Django ORM call manager method from another manager?

Any tips on how I should be accessing other manager methods from within Manager.py?

No matter what I do, I can't seem to access my other manager's method. Python complains that it isn't defined...

Is it going to cause problems if I import models inside of managers.py? Circular includes or whatever?

managers.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 

You need to change:

from myir import models

to

from myir.models import DependentPatientID

The reason being, you might have already done from django.db import models and the names are conflicting.

Now,

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

returns an object, and not a queryset. So, DependentPatientID.objects.get_patient_ids(d)[0] would fail.

So try this

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 = []

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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