djongo
djongo copied to clipboard
filter(contains or startswith) for embedded model is returning empty array
Filter contains for embedded model is returning empty array
Python script
models.py
from djongo import models
class User(models.Model):
user_name=models.CharField(max_length=120,help_text='User Name')
user_id=models.CharField(max_length=120,help_text='User Id')
objects=models.DjongoManager()
class Meta:
abstract = True
def __str__(self):
return self.user_id
class Sentences(models.Model):
_id=models.ObjectIdField()
input_sentence=models.TextField(help_text='Input Sentence')
user = models.EmbeddedField(model_container=User)
timestamp = models.DateTimeField(auto_now=True)
objects=models.DjongoManager()
def __str__(self):
return str(self._id)
in views.py
Sentences.objects.filter(user={'username':'sampleuser'}) is working
But
Sentences.objects.filter(user__contains={'username':'sample'}) returns empty array
Traceback
It forms query,
"$match": { "user": { "$regex": "^.{'user\_name': 'venu'}.$" } }
instead of
"$match": { "user.user_name": { "$regex": "^.venu.$" } }
Django = 2.2.11 djongo=1.3.1 mongodb=3.6.3
Your query is defined incorrectly. This should work (I'm assuming you're searching for User documents that have field user_name which contains "sample")
user__user_name__contains="sample"
@SebastianRemander but it throws,
django.core.exceptions.FieldError: Unsupported lookup 'user_name' for EmbeddedField or join on the field not permitted.
Sentences.objects.filter(user__user_name__contains='sample')
ok :thinking: That error seems to be a problem with django, and not djongo.. Are you using MongoDB 3.6 or higher, as mentioned in the readme? (required for that type of queries)
@SebastianRemander I am using the following versions,
Django = 2.2.11 djongo=1.3.1 mongodb=3.6.3
@SebastianRemander I used the code querying-embedded-fields as mentioned in this document
I'm also experiencing the same issue.
Same issue here.
I have these models defined:
from djongo import models
class Blog(models.Model):
name = models.CharField(max_length=100)
class Meta:
abstract = True
class Entry(models.Model):
blog = models.EmbeddedField(
model_container=Blog
)
headline = models.CharField(max_length=255, unique=True)
objects = models.DjongoManager()
def __str__(self):
return self.headline
And when I run:
from mongodb import models
models.Entry.objects.filter(blog__name='Djongo')
It throws this error: django.core.exceptions.FieldError: Unsupported lookup 'name' for EmbeddedField or join on the field not permitted.
I can do a 'match' query but if I try to use either startswith or contains:
>>> models.Entry.objects.filter(blog={"name": "Djongo"}) # Match query works as expected
<QuerySet [<Entry: The Django MongoDB connector>, <Entry: The Django MongoDB connector>]>
>>> models.Entry.objects.filter(blog__startswith={"name": "Djo"}) # startswith does not work
<QuerySet []>
>>> models.Entry.objects.filter(blog__contains={"name": "Djo"}) # contains does not work
<QuerySet []>
Edit:
Just tried using aggregate functions and it works as expected as well.
I guess this is can be used as a workaround for now:
>>> _aggregate = models.Entry.objects.mongo_aggregate([
... {
... "$match": {
... 'blog.name': {
... '$regex': '^Djo.*$'
... }
... }
... }
... ])
>>>
>>> _aggregate.next()
OrderedDict([('_id', ObjectId('6071d74a9b0f8139b7624a8d')), ('blog', OrderedDict([('name', 'Djongo')])), ('headline', 'The Django MongoDB connector')])
Is this issue be fixed?
Same thing is happening to me