[英]Django ORM call manager method from another manager?
Any tips on how I should be accessing other manager methods from within Manager.py? 关于如何从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... Python抱怨它没有定义...
Is it going to cause problems if I import models inside of managers.py? 如果我在manager.py中导入模型,会引起问题吗? Circular includes or whatever? 通函包括什么?
managers.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
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. 原因是,您可能已经from django.db import models
完成操作from django.db import models
并且名称冲突。
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. 因此, DependentPatientID.objects.get_patient_ids(d)[0]
将失败。
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 = []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.