0

I am trying to build a webapp with flask and mongoengine, but get stuck with the following problem.

Assume there is a document with JSON structure like this:

"user": {
  "value": "username",
  [...]
},
"entities": [
  {
    "key": "foo",
    "value": "123"
  }, 
    "key": "bar",
    "value": "456"
  },
  [...]
]

1) First I want to get the whole document from user "username", I tried:

userdata = models.User.objects(user__match = {"value": "username"})

but this return an empty doc. I also tried something like this:

userdata = models.User.objects(user__contains = "username")

But then I got: AttributeError: 'str' object has no attribute 'get'

I read the docs, and sneaked around but found nothing helpful.

2) When the document got (finally) from the DB, I need to catch all the values from the entities for displaying that within an jinja2 template, but have no clue how to managed that.

This is (part of) the model:

class Username(EmbeddedDocument):
    value = StringField(
        primary_key = True,
        unique = True)
    created = DateTimeField(
        require = True,
        default = datetime.utcnow())

class Entities(EmbeddedDocument):
    etype = StringField(
        require = True,
        choices = ENTITYTYPES)
    key = StringField(
        require = True,
        choices = ENTITIES)
    value = StringField(
        require = True)
    modiefied = DateTimeField(
        require = True,
        default = datetime.utcnow())

class User(Document):
    user = EmbeddedDocumentField(
        Username)
    entities = ListField(
        GenericEmbeddedDocumentField(Entities))

As mentioned from @Paul I tried some queries:

userdata = models.User.objects.get(entities={"key": "foo"})  

Result: AttributeError: 'dict' object has no attribute 'to_mongo'

userdata = models.User.objects(entities={"key": "foo"})  

Result: AttributeError: 'dict' object has no attribute 'to_mongo'

userdata = models.User.objects(user__entities = {"key": "foo"})  

Result: mongoengine.errors.InvalidQueryError: Cannot resolve field "user"

userdata = models.User.objects.get(entities={"key": "foo"})  

Result: AttributeError: 'dict' object has no attribute 'to_mongo'

userdata = models.User.objects(__raw__={"key": "foo"})  

Result: []

userdata = models.User.objects(key='foo')  

Result: mongoengine.errors.InvalidQueryError: Cannot resolve field "key"

ic14
  • 31
  • 6

2 Answers2

2

While further searching around, I came across this Question and this.

Putting them together brought this answer for my first question:
data = models.User.objects(__raw__ = { 'user.value': 'username' })

So I get the data from the user. To sort it out in jinja2, I found that Question and that and so I came to this template:

{% for dict_item in data %}
  {% for item in dict_item['entities'] %}
    <h1>Key: {{item['key']}}</h1>
    <h2>Value: {{item['value']}}</h2>
  {% endfor %}
{% endfor %}

Just for documentation: If you need to print your mongoengine query in the shell, use this:

from bson import json_util
objects = models.User.objects.all()
json_util.dumps(objects._collection_obj.find(objects._query))
ic14
  • 31
  • 6
0

Am i right, that Model User has a field user, and that field is a dict? What if you try this:

userdata = models.User.objects.get(user={"value": "username"}) # will return one
userdata = models.User.objects(user={"value": "username"})     # will return multiple

Or this:

userdata = models.User.objects(user__user = {"value": "username"})
Paul
  • 6,641
  • 8
  • 41
  • 56
  • Thanks for mentioning. But it is not working :-( `models.User.objects.get(..)` brings up `app.models.DoesNotExist: User matching query does not exist.` `models.User.objects(...)` is an empty result set `models.User.objects(user__user = (...))` generate `mongoengine.errors.InvalidQueryError: Cannot resolve field "user"` This stuff drives me crazy :) – ic14 Feb 20 '16 at 21:23