Closed KeironO closed 7 years ago
Issues with the Document schema are now fixed, new error being returned.
from mongoengine import *
class Positive(EmbeddedDocument):
count = IntField()
peaks = StringField() # Temporary
class Negative(EmbeddedDocument):
count = IntField()
peaks = StringField() # Temporary
class AdductWeights(EmbeddedDocument):
negative = EmbeddedDocumentField(Negative)
positive = EmbeddedDocumentField(Positive)
neutral = FloatField()
class MetaboliteAdduct(Document):
meta = { "collection" : "metabolites"}
name = StringField()
adduct_weights = EmbeddedDocumentField(AdductWeights)
Now returns AttributeError: type object 'AdductWeights' has no attribute 'objects'
.
Traceback (most recent call last):
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/keo7/.local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/mimerender.py", line 244, in wrapper
result = target(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/flask_mongorest/views.py", line 56, in dispatch_request
return self._dispatch_request(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/flask_mongorest/views.py", line 68, in _dispatch_request
return super(ResourceView, self).dispatch_request(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/flask_views/base.py", line 36, in dispatch_request
return super(View, self).dispatch_request(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/flask/views.py", line 149, in dispatch_request
return meth(*args, **kwargs)
File "/home/keo7/.local/lib/python2.7/site-packages/flask_mongorest/views.py", line 107, in get
result = self._resource.get_objects(qfilter=qfilter)
File "/home/keo7/.local/lib/python2.7/site-packages/flask_mongorest/resources.py", line 756, in get_objects
qs = self.get_queryset()
File "/home/keo7/.local/lib/python2.7/site-packages/flask_mongorest/resources.py", line 546, in get_queryset
return self.document.objects
AttributeError: type object 'AdductWeights' has no attribute 'objects'
Are you using AdductWeights
as a document
in your resource? EmbeddedDocument
s can't be used this way, only Document
classes. EmbeddedDoc.objects
indeed doesn't exist.
These are my resources.
class PositiveResource(Resource):
document = d.Positive
class NegativeResource(Resource):
document = d.Negative
class AdductWeightsResource(Resource):
document = d.AdductWeights
related_resources = {
"positive" : PositiveResource,
"negative" : NegativeResource
}
class MetaboliteAdductResource(Resource):
document = d.MetaboliteAdduct
related_resources = {
"adduct_weight" : AdductWeightsResource
}
How else am I supposed to be using them?
First of all, thanks @KeironO for uncovering all the things that should be covered by the documentation. Appreciate your inquiries :)
Change "adduct_weight"
to "adduct_weights"
in related_resources
.
Edit: Removed previous invalid answer.
Note to self: typo in the field name in related_resources
should raise a more obvious error.
@wojcikstefan not a problem, once I get my head around it I'll probably go about creating a simple-CMS to demonstrate said functionality simplistically for others.
Even when I change the "related_resources" field name, the same error is given.
Documents:
class Positive(EmbeddedDocument):
count = IntField()
peaks = StringField() # Temporary
class Negative(EmbeddedDocument):
count = IntField()
peaks = StringField() # Temporary
class AdductWeights(EmbeddedDocument):
negative = EmbeddedDocumentField(Negative)
positive = EmbeddedDocumentField(Positive)
neutral = FloatField()
class MetaboliteAdduct(Document):
meta = {"collection" : "metabolites"}
name = StringField()
adduct_weights = EmbeddedDocumentField(AdductWeights)
Resources
class PositiveResource(Resource):
document = d.Positive
class NegativeResource(Resource):
document = d.Negative
class AdductWeightsResource(Resource):
document = d.AdductWeights
related_resources = {
"positive" : PositiveResource,
"negative" : NegativeResource
}
class MetaboliteAdductResource(Resource):
document = d.MetaboliteAdduct
related_resources = {
"adduct_weights" : AdductWeightsResource
}
Update: I managed to fix that issue, I'm just struggling to get the operators to work.
Documents:
class PositiveAdduct(EmbeddedDocument):
count = IntField()
peaks = StringField()
class NegativeAdduct(EmbeddedDocument):
count = IntField()
peaks = StringField()
class AdductWeights(EmbeddedDocument):
neutral = FloatField()
positive = EmbeddedDocumentField(PositiveAdduct)
negative = EmbeddedDocumentField(NegativeAdduct)
class MetaboliteAdduct(DynamicDocument):
meta = {"collection" : "metabolites"}
name = StringField()
adduct_weights = EmbeddedDocumentField(AdductWeights)
Resources:
class NegativeAdductResource(Resource):
document = d.NegativeAdduct
filters = {
"count" : [ops.Gt]
}
class PositiveAdductResource(Resource):
document = d.PositiveAdduct
class AdductWeightsResource(Resource):
document = d.AdductWeights
related_resources = {
"positive" : PositiveAdductResource,
"negative" : NegativeAdductResource
}
class MetaboliteAdductResource(Resource):
document = d.MetaboliteAdduct
filters = {
"name" : [ops.Exact]
}
related_resources = {
"adduct_weights" : AdductWeightsResource
}
When I attempt to query the API using the following request:
adducts/?adduct_weights_negative_count__gt=0
Nothing seems to happen!
@KeironO remove the filters from the sub-resources and on the main MetaboliteAdductResource
add:
filters = {
'adduct_weights__positive__count': [ops.Gt],
'adduct_weights__negative__count': [ops.Gt]
}
Then, you can query it by doing e.g. GET /metabolite/?adduct_weights__negative__count__gt=2
. Note that subfields are separated from fields the same way as operators - by double underscores.
I owe you a beer!
Haha, no problem mate. Thanks for pointing out some parts where Flask-MongoRest is not intuitive nor documented well. It's very helpful to see the problems through a new adopter's eyes!
I think we can close this issue now. I renamed it so that it's easier to find by others.
Following up from https://github.com/closeio/flask-mongorest/issues/103, I am experiencing an issue involving the use of an EmbeddedDocument.
My MongoDB collection currently resembles the following:
If you look at the
adduct_weights
key, it holds a collection resembling:Following the example provided within this repository, I have written the following Documents and Resources.
No error is being thrown when I query
MetaboliteAdductView
's url, however no data is being returned either.Where have I gone wrong here?