Git Product home page Git Product logo

Comments (9)

KeironO avatar KeironO commented on August 16, 2024 1

I owe you a beer!

from flask-mongorest.

KeironO avatar KeironO commented on August 16, 2024

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'

from flask-mongorest.

wojcikstefan avatar wojcikstefan commented on August 16, 2024

Are you using AdductWeights as a document in your resource? EmbeddedDocuments can't be used this way, only Document classes. EmbeddedDoc.objects indeed doesn't exist.

from flask-mongorest.

KeironO avatar KeironO commented on August 16, 2024

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?

from flask-mongorest.

wojcikstefan avatar wojcikstefan commented on August 16, 2024

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.

from flask-mongorest.

KeironO avatar KeironO commented on August 16, 2024

@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
    }

from flask-mongorest.

KeironO avatar KeironO commented on August 16, 2024

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!

from flask-mongorest.

wojcikstefan avatar wojcikstefan commented on August 16, 2024

@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.

from flask-mongorest.

wojcikstefan avatar wojcikstefan commented on August 16, 2024

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.

from flask-mongorest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.