Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:17:35+00:00 2026-06-11T21:17:35+00:00

i have a problem. This is how im trying to extend default User model:

  • 0

i have a problem.
This is how im trying to extend default User model:

# myapp models.py
from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    games = None

def create_user_profile(sender, instance, created, **kwargs):
    if not created:
        profile, created = UserProfile.objects.get_or_create(user=instance)

models.signals.post_save.connect(create_user_profile, sender=User)

Now i want to change ‘games’ attr when loggin in:

# myapp views.py
from django.views.generic.edit import FormView
from django.contrib.auth.forms import AuthenticationForm
class LoginView(FormView):
    form_class = AuthenticationForm
    template_name = 'registration/login.html'

    def form_valid(self, form):
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                # default value for games is None
                user.userprofile.games = {}
                # now it should be an empty dict
                login(self.request, user)
                return redirect('/game')

class Index(FormView):
    def dispatch(self, request, *args, **kwargs):
        profile = request.user.get_profile()
        print profile.games # Prints 'None'

Well, my questions are:
Why ‘print profile.games’ prints ‘None’ and How can i change games attr when loggin in?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:17:36+00:00Added an answer on June 11, 2026 at 9:17 pm

    I don’t think that is a way to create field in the model. You need to do it as:

    game = models.CharField(max_length=300, null=True, blank=True)
    

    And reset it to None or dict you want to store each time you login and save it.

    In your login view:

    import json
    class LoginView(FormView):
        ....
        #your code
        if user.is_active:
            # default value for games is None
            user.userprofile.games = json.dumps({}) # some dict you want to store
            user.userprofile.save()   #save it
    
            # now it should be an empty dict
            login(self.request, user)
            return redirect('/game')
        ....
        #your other code
    

    Problem with your code is that, value of game is not stored in DB. It is just an attribute of and instance. So it will not be preserved over different instances and every time you get an instance it will be reset to ‘None. InIndexview you are getting new instance ofuserprofilewhich has 'game set to None.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with this query trying to get a sum and a
I have this problem when trying to run hello world program using android SDK.
I have this problem I've been trying to tackle for a while. I have
Ok I have this problem I'm trying to use Jquery to load a partial
I have a huge problem with this method im trying to make. I have
So we have this problem that we are trying to figure out. Heres what
Basically i have a problem with this timer program I am trying to put
I am wondering why I have this problem with my list. I am trying
I'm trying to calculate TimeSpans between dates. I have no problem with this if
I have been trying to solve this problem for a while, but couldn't with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.