I'm using Django 3.0.6 with Django Rest Framework 3.11.1. I'm connecting to a MongoDB database using the djongo connector. One of my models has an ArrayField that contains embedded records. I would like to know how I can retrieve these embedded fields using a django query.
Each Person can have many different sub records. Here is a sample model to illustrate what I'm working on
Models:
from djongo import models
class SubRecords(models.Model):
status = models.CharField(max_length=20)
startTime = models.CharField(max_length=20)
identifier = models.CharField(max_length=20)
job_title = models.CharField(max_length=20)
class Meta:
abstract = True
class Person(models.Model):
_id = ObjectIdField()
workplace = models.CharField(max_length=120)
subject = models.CharField(max_length=120)
records = models.ArrayField(model_container=SubRecords)
I would like to query the Person
model and get
Person.records
objects andPerson.records
objects that match some criteriaI have tried to do this
>>> Person.objects.filter(records__exact={'job_title': 'HR'})
Now the problem I'm facing is that the result isn't limited to subrecords where the job title is HR, instead if a Person
object contains a sub record that matches the criteria, the whole Person object and associated sub records are returned.
I want to be able to get a list of all the subrecords and only the subrecords that match the criteria I specify. How can I do this?
You don't need __exact:
Person.objects.filter(records={'job_title':'HR'})