miguelgrinberg / microblog-api

A modern (as of 2024) Flask API back end.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need help with paginator decorater.

jsnyder10 opened this issue · comments

Hi Miguel. Thanks for open sourcing the microblog-api project. By any chance do you have a tutorial for it? If not I am trying to get rid of the "data" entry along with the object format on top. I can't figure out how to do this. Do you have any suggestions?

Format wanted:

 [
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.691223", 
      "icon": "FruitsVegetable", 
      "id": 1, 
      "images": [], 
      "language": "en", 
      "name": "Grocery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": true, 
        "layoutType": "classic", 
        "productCard": "neon"
      }, 
      "slug": "grocery", 
      "translated_languages": [
        "es", 
        "fr", 
        "de"
      ], 
      "updated_at": "2022-10-03T00:20:16.691223"
    }, 
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.693224", 
      "icon": "Bakery", 
      "id": 2, 
      "images": [], 
      "name": "Bakery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": false, 
        "layoutType": "standard", 
        "productCard": "argon"
      }, 
      "slug": "bakery", 
      "translated_languages": [], 
      "updated_at": "2022-10-03T00:20:16.693224"
    }, 
    ]

Using the paginated decorator you made I get this:

{
  "data": [
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.691223", 
      "icon": "FruitsVegetable", 
      "id": 1, 
      "images": [], 
      "language": "en", 
      "name": "Grocery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": true, 
        "layoutType": "classic", 
        "productCard": "neon"
      }, 
      "slug": "grocery", 
      "translated_languages": [
        "es", 
        "fr", 
        "de"
      ], 
      "updated_at": "2022-10-03T00:20:16.691223"
    }, 
    {
      "banners": [], 
      "created_at": "2022-10-03T00:20:16.693224", 
      "icon": "Bakery", 
      "id": 2, 
      "images": [], 
      "name": "Bakery", 
      "promotional_sliders": [], 
      "settings": {
        "isHome": false, 
        "layoutType": "standard", 
        "productCard": "argon"
      }, 
      "slug": "bakery", 
      "translated_languages": [], 
      "updated_at": "2022-10-03T00:20:16.693224"
    }, 
    ]
}

If you don't want to have pagination information, then you shouldn't use the paginated decorator. Create a schema for the payload that you want to return, put it in the @response decorator, and then make sure your endpoint returns data that is compatible with that schema.

Thanks for the answer Miguel. I don't know why all my dictionaries are turned into empty dictionaries when passing through the wrappers.

Here is a route I made that uses @response, but when it passes to the browser I get {}

@types.route('/types3', methods=['GET'])
@authenticate(token_auth)
@response(type_schema2)
def get():
"""Return all types"""
types = db.session.scalars(Type.select()).all()
print(types)
return types

I did the print to show my dicitonary query formatted properly before it got to the return. Here is what the console shows from my print statement:
[<Type 1>, <Type 2>, <Type 3>, <Type 4>, <Type 5>, <Type 6>, <Type 7>, <Type 8>]

Got any ideas?

I dug through the docs and never found the answer. After compiling someone else's example I found I just needed to put many=True inside my TypeSchema at the top of my routes type_schema = TypeSchema(many=True).

I really appreciate all the tutorials you've made. I've gone through your flask mega tutorial and I bought your react mega tutorial. You've been a big help on my programming journey.