how to store third party access token to database to my profile model from views.py django

  django, python

i’m trying to store a session id and an access token from a third party website’s API to my users profiles
i’ve been trying for days and searching but i couldn’t find anything like this

i tried to import profiles instances so that i can save the data to the users profiles instances
but i didn’t find out how , here is my code

#views.py
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
from django.contrib.auth.decorators import login_required
import urllib.parse
from django.views.generic import View
from users.models import Profile
from django.contrib.auth.models import User

def profile_view(request):
    RuName= 'driss_aitkassi-drissait-crm-SB-ijuzzy'

    r= {
    'RuName': RuName
    }

    response = api.execute('GetSessionID', r)
    
    print(response)

    res =response.text

    tree= ET.fromstring(res)

    #here is my session id from the website's api    

    for node in tree.iter('{urn:ebay:apis:eBLBaseComponents}GetSessionIDResponse'):
        SessionID= node.find('{urn:ebay:apis:eBLBaseComponents}SessionID').text 
    #i tried this :

    s=Profile(session_id=SessionID)
    s.save()

    # and also tried this
        

    # and also this:

    Profile.objects.update(session_id=SessionID)
    
    Profile.objects.model.save()

here is the Models.py file:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


class Profile(models.Model):
    user = models.OneToOneField(User,related_name='profile' , on_delete=models.CASCADE)
    token = models.TextField(max_length=500, blank=True)
    session_id=models.TextField( max_length=500, blank=True)
    def __str__(self):
        return self.user.username

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

please help me with this or just point me at the right direction thanks

Source: Python Questions

LEAVE A COMMENT