Cl0udSt0ne
Cl0udSt0ne

Reputation: 538

Flask-MongoEngine AttributeError: 'QuerySet' object has no attribute 'get_or_404'

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

Upvotes: 1

Views: 4685

Answers (4)

javad_sh
javad_sh

Reputation: 29

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

Upvotes: 2

laurajaime
laurajaime

Reputation: 379

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.

Upvotes: 0

MichaelWClark
MichaelWClark

Reputation: 390

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}

Upvotes: 0

Alexey Smirnov
Alexey Smirnov

Reputation: 2613

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

Upvotes: 0

Related Questions