Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

TypeError: 'modelname' object is not subscriptable #125

Closed
2 tasks done
foxmask opened this issue Oct 28, 2021 · 4 comments
Closed
2 tasks done

TypeError: 'modelname' object is not subscriptable #125

foxmask opened this issue Oct 28, 2021 · 4 comments
Labels
question Further information is requested

Comments

@foxmask
Copy link
Contributor

foxmask commented Oct 28, 2021

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

can't read the content of the model

To reproduce

try the quickstart of the doc

import databases
import orm
import asyncio

database = databases.Database("sqlite:///db.sqlite")
models = orm.ModelRegistry(database=database)


class Note(orm.Model):
    tablename = "notes"
    registry = models
    fields = {
        "id": orm.Integer(primary_key=True),
        "text": orm.String(max_length=100),
        "completed": orm.Boolean(default=False),
    }

# Create the database and tables
# models.create_all()

async def get_all():
    data = await Note.objects.get(id=1)
    print(data)
    data = await Note.objects.all()
    print(data)
    for result in data:
        print(result['id'])

asyncio.run(get_all())
11:58 $ python models_test.py 
<__main__.Note object at 0x7f7dcdfc51f0>
[<__main__.Note object at 0x7f7dcdfc5760>, <__main__.Note object at 0x7f7dcdfc5820>]
Traceback (most recent call last):
  File "models_test.py", line 29, in <module>
    asyncio.run(get_all())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "models_test.py", line 27, in get_all
    print(result['id'])
TypeError: 'Note' object is not subscriptable

Expected behavior

I would like ORM works as before :)

Actual behavior

return object instead of data

the doc says

note = await Note.objects.get(id=1)
print(note)
# Note(id=1, text="Buy the groceries.", completed=False)

but I get

    data = await Trigger.objects.get(id=1)
    print(data)
<yeoboseyo.models.Trigger object at 0x7f997488bac0>

so then getting .all()

    data = await Trigger.objects.all()
    print(data)
    for result in data:
        print(result['id'])

[<yeoboseyo.models.Trigger object at 0x7f997488bac0>]
    print(result['id'])
TypeError: 'Trigger' object is not subscriptable

Debugging material

(yeoboseyo) ✔ ~/Projects/yeoboseyo/yeoboseyo/yeoboseyo [master|✚ 6…11] 11:35 $ python app.py 여보세요 ! INFO: Started server process [25851] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

INFO: 127.0.0.1:48144 - "GET / HTTP/1.1" 200 OK
INFO: 127.0.0.1:48144 - "GET /static/call-me-hand.png HTTP/1.1" 304 Not Modified
<yeoboseyo.models.Trigger object at 0x7f6e2b64dee0>
INFO: 127.0.0.1:48144 - "GET /api/yeoboseyo/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in call
return await self.app(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/applications.py", line 112, in call
await self.middleware_stack(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in call
raise exc
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in call
await self.app(scope, receive, _send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in call
raise exc
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in call
await self.app(scope, receive, sender)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 656, in call
await route.handle(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 408, in handle
await self.app(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 656, in call
await route.handle(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 408, in handle
await self.app(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 656, in call
await route.handle(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/home/foxmask/Projects/yeoboseyo/lib/python3.8/site-packages/starlette/routing.py", line 61, in app
response = await func(request)
File "app.py", line 43, in get_all
for result in data:
TypeError: 'Trigger' object is not iterable

Environment

  • OS: Linux
  • Python 3.8.10
  • ORM version: 0.2.1

Additional context

the same code worked in 0.1.8 (https://framagit.org/annyong/yeoboseyo/-/blob/master/yeoboseyo/models.py)

@aminalaee
Copy link
Member

Hi,
I'm not sure how this was working, but shouldn't you be using note.id instead of note['id'] ?
Can you point to the docs where this is mentioned?

@foxmask
Copy link
Contributor Author

foxmask commented Oct 28, 2021

Hi,
thanks for your response.
Doc does not mentioned that ['id'], and it's ok with .id
At least remain a imprecision may be with

print(note)
# Note(id=1, text="Buy the groceries.", completed=False)

as it displayed for example

<__main__.Note object at 0x7f3d8c03b1c0>

but not

 Note(id=1, text="Buy the groceries.", completed=False)

THanks again I can continue to update my project with this new ORM version

@aminalaee
Copy link
Member

You're right about that. I'll create a separate issue for that.

Thank for informing it. And feel free to create PRs for thet.

@aminalaee aminalaee added the question Further information is requested label Oct 28, 2021
@aminalaee
Copy link
Member

Created #126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants