ali-zahedi / django-telethon

Integrate Django with Telethon(Pure Python 3 MTProto API Telegram client library, for bots too!).
MIT License
85 stars 11 forks source link

Field 'id' expected a number but got <ClientSession: Sesion1> #2

Closed NoxCreation closed 1 year ago

NoxCreation commented 1 year ago

Hi, I already logged in with send-code-request and send-code-request. The session is already in DB. But when I try:

....
cs = ClientSession.objects.update_or_create(name='Sesion1')
session = DjangoSession(client_session=cs)
telegram_client = TelegramClient(session, api_id=int(app.api_id), api_hash=app.api_hash)
....

I get the following error back: Field 'id' expected a number but got

What can it be?

Jtobyy commented 1 year ago

Were you able to work around this?

I was able to get the id with

cs = ClientSession.objects.update_or_create(name=phone) cs = ClientSession.objects.get(name=phone) cs.id 2

So using ClientSession.objects.get(name=phone) instead of ClientSession.objects.update_or_create(name=phone)

ali-zahedi commented 1 year ago

@Jtobyy @MoonMagiCreation

The issue seems to be in your usage of Django's update_or_create() method. This method actually returns a tuple that contains the object and a boolean value indicating whether the object was created or not.

So, in your case, when you use:

cs = ClientSession.objects.update_or_create(name='Sesion1')

The variable cs is a tuple like (ClientSession object, True/False). It means you're trying to pass this tuple into DjangoSession(client_session=cs), which is causing the issue because client_session expects a ClientSession instance, not a tuple.

You need to unpack this tuple. Modify your code to this:

cs, created = ClientSession.objects.update_or_create(name='Sesion1')
session = DjangoSession(client_session=cs)
telegram_client = TelegramClient(session, api_id=app.api_id, api_hash=app.api_hash)

In this case, cs will be the ClientSession object that you want, and created will be a boolean indicating whether a new object was created (True) or an existing one was updated (False).

Jtobyy commented 1 year ago

Thank you for the clarification.