Skip to content

Issues with MapField and _fields_to_dbfields #1275

@AlJohri

Description

@AlJohri

I have a model defined with a MapField.

class Race(Document):
    raceUID = StringField(primary_key=True)
    electionDate = StringField()
    statePostal = StringField()

    historical_reporting_units = MapField(ListField(EmbeddedDocumentField(ReportingUnit)))
    current_reporting_units = MapField(field=EmbeddedDocumentField(ReportingUnit))

When trying to project the result to only include a certain key within the MapField like so: Race.objects(raceUID="2016-03-01-MA-24548").only("current_reporting_units.state-MA").first()

I get the following stack trace. I believe it should skip trying to resolve the dbfield attribute for MapFields.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-9098a246470f> in <module>()
      1 from electionbot2016.models import Race, ReportingUnit
----> 2 race = Race.objects(raceUID="2016-03-01-MA-24548").only("current_reporting_units.state-MA").first()
      3 for unit in race.current_reporting_units:
      4     print(unit)

/Users/johria/anaconda/lib/python3.5/site-packages/mongoengine/queryset/base.py in only(self, *fields)
    810         fields = dict([(f, QueryFieldList.ONLY) for f in fields])
    811         self.only_fields = list(fields.keys())
--> 812         return self.fields(True, **fields)
    813 
    814     def exclude(self, *fields):

/Users/johria/anaconda/lib/python3.5/site-packages/mongoengine/queryset/base.py in fields(self, _only_called, **kwargs)
    864         for value, group in itertools.groupby(fields, lambda x: x[1]):
    865             fields = [field for field, value in group]
--> 866             fields = queryset._fields_to_dbfields(fields)
    867             queryset._loaded_fields += QueryFieldList(
    868                 fields, value=value, _only_called=_only_called)

/Users/johria/anaconda/lib/python3.5/site-packages/mongoengine/queryset/base.py in _fields_to_dbfields(self, fields)
   1659             try:
   1660                 field = ".".join(f.db_field for f in
-> 1661                                  document._lookup_field(field.split('.')))
   1662                 ret.append(field)
   1663             except LookUpError as err:

/Users/johria/anaconda/lib/python3.5/site-packages/mongoengine/queryset/base.py in <genexpr>(.0)
   1658         for field in fields:
   1659             try:
-> 1660                 field = ".".join(f.db_field for f in
   1661                                  document._lookup_field(field.split('.')))
   1662                 ret.append(field)

AttributeError: 'str' object has no attribute 'db_field'

Am I using the only function wrong? Can I manually input a raw projection for the timebeing?

EDIT: added current_reporting_units to Race model

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions