1

I followed Flask-MongoEngine tutorial and use the code below:

tag = Tag.objects.get_or_404(slug=tag_slug)

it raised an AttriubteError:

AttributeError: 'QuerySet' object has no attribute 'get_or_404'

my pip freeze:

mongoengine==0.11.0
pymongo==3.4.0
Flask==0.12
flask-mongoengine==0.8.2
Flask-WTF==0.14

4 Answers 4

2

Just remove ‍mongoengine from your pip freeze and in model definition import Document from flask_mongoengine, not from mongoengine.

Sign up to request clarification or add additional context in comments.

Comments

0

Try it like that tag = Tag.objects().get_or_404(slug=tag_slug)

Comments

0

You need to add BaseQuerySet as the "queryset_class"

WRONG:

import mongoengine
from mongoengine import Document

db = mongoengine

class Tag(db.Document):
    field = db.StringField()

    meta = { 'collection': 'tags' }

RIGHT:

import mongoengine
from flask_mongoengine import BaseQuerySet
from mongoengine import Document

db = mongoengine

class Tag(db.Document):
    field = db.StringField()

    meta = { 'collection': 'tags', 'queryset_class': BaseQuerySet}

Comments

0

Could you attach the Tag class to the model scheme?

Maybe you have an error in model file.

You could create a model file that includes a Tag class to test if the get_or_404 method works now.

model.py

from mongoengine import *

class Tag(Document):
  slug = StringField()
  name = StringField()

  ....
  other attributes

And maybe now you can do it that:

def slug(tag_slug):
  tag = Tag.objects.get_or_404(slug=tag_slug)

Try it and you tell us.

2 Comments

I'm sorry if my English is bad... I was just trying to help. :(
If this is in fact an answer, please expand by posting the details of what you're proposing that might solve the original poster's problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.