Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

AttributeError AttributeError: 'Ligas' object has no attribute 'to_dict' #18

Open
cleberlup opened this issue Sep 23, 2022 · 1 comment

Comments

@cleberlup
Copy link

Olá, estou iniciando o aprendizado com python e flask, e estou tendo dificuldades para fazer a chamada da api devido este erro, poderiam ajudar?
Abaixo o trecho do código com o erro, as variáveis tem nomes diferentes, mas fazem a mesma coisa.

from flask import abort, jsonify
from flask_restful import Resource
from ext.database import Ligas

class LigasResource(Resource):
    def get(self):
        ligas = Ligas.query.all() or abort(204)
        return jsonify(
            {'liga': [dic_liga.to_dict() for dic_liga in ligas]}
        )


class LigasItemResource(Resource):
    def get(self, liga_id):
        liga = Ligas.query.filter_by(id=liga_id).first() or abort(404)
        return jsonify(liga.to_dict())

database.py:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Ligas(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(140))
    description = db.Column(db.Text)

def init_app(app):
    db.init_app(app)

obs: já vi o video umas 50 vezes e o conteúdo é fantastico, parabens!!!
Se este não for o canal correto para isto, me desculpe rsrsrs.. Obrigado

@santosigor45
Copy link

Você precisa adicionar SerializerMixin da biblioteca sqlalchemy_serializer em seus modelos.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants