Skip to content

Commit

Permalink
Source/Collection | summary | optimizing queryset to get count
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Jun 26, 2021
1 parent 53502e6 commit 5be98b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ def is_openmrs_schema(self):

@property
def active_concepts(self):
return self.concepts.filter(retired=False, is_active=True).distinct('versioned_object_id').count()
return self.concepts.filter(retired=False, is_active=True).count()

@property
def active_mappings(self):
return self.mappings.filter(retired=False, is_active=True).distinct('versioned_object_id').count()
return self.mappings.filter(retired=False, is_active=True).count()

@property
def last_concept_update(self):
Expand Down
14 changes: 14 additions & 0 deletions core/sources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,17 @@ def hierarchy(self, offset=0, limit=100):
offset=offset,
limit=limit
)

@property
def active_concepts(self):
queryset = self.concepts.filter(retired=False, is_active=True)
if self.is_head:
queryset = queryset.filter(is_latest_version=True)
return queryset.count()

@property
def active_mappings(self):
queryset = self.mappings.filter(retired=False, is_active=True)
if self.is_head:
queryset = queryset.filter(is_latest_version=True)
return queryset.count()

0 comments on commit 5be98b8

Please sign in to comment.