Din
Din

Reputation: 71

Updating value of a model

I am newbie of Django. And I don't understand how Django model works.

First, I am trying to create a model of user score in users/models.py, so each user has a score variable to store their own score, and score can be updated.

Then, in my morse_logs/game1.html, i will ask a question as simple as 1+1 to user, then i retrieve the answer and compare it in morse_logs/views.py to see if it is correct, if it is correct, I will add 5 score to that user; if incorrect, no score is added.

So now I am struggling to define a correct model for setting a field for user to have their own score, and I wonder how can I update and retrieve the score of the user in view.py.

users/models.py

from django.db import models
from django import forms
from django.contrib.auth.models import User
from django.db.models.signals import post_save

# Create your models here.


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    description = models.CharField(max_length=100, default='')
    #score = models.CharField(User, max_length=10, default=0)


def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = UserProfile.objects.create(user=kwargs['instance'])

    post_save.connect(create_profile, sender=User)


class UserScore(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    score = models.CharField(max_length=1000, default=0)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Returns a string representation of the model"""
        return self.score

morse_logs/game1.html

{% block content %}
<div class="container">
    <title>GAME 1</title>
    <h1>GAME 1</h1>
    <h2>1 + 1 = ?</h2>
    <input type="number" id="ans1" name="ans1"><br><br>
    <button type="submit" name="game1Answer">Decipher</button>

</div>
{% endblock content %}

morse_logs/views.py

@login_required()
def game1(request):
    """The Game 1 page"""
    val1 = request.GET.get('ans1', '')
    res = "Incorrect"

    if val1 == 2:
        #user's score declared in model increase 5points
        #display correct and 5 points added to user
        res = "Correct"
    else:
        #user's score declared in model has no point
        #display incorrect and 0 point added to user
        res = "Incorrect"

    return render(request, 'morse_logs/game1.html', {'result': res})

Upvotes: 1

Views: 96

Answers (2)

Micheal J. Roberts
Micheal J. Roberts

Reputation: 4180

Firstly, you have probably the wrong field type for storing this store information.

I would go with an IntegerField:

score = models.IntegerField(default=0)

Because you have changed from a CharField to an IntegerField, you will need to run python manage.py makemigrations and python manage.py migrate to resolve your database with the changes.

Then, in your views you would want something like this, utilising the get_or_create method for looking up an object with the given **kwargs (may be empty if your model has defaults for all fields), creating one if necessary.

Reference here: https://kite.com/python/docs/django.db.models.QuerySet.get_or_create

@login_required()
def game1(request):

    if request.user and not request.user.is_anonymous:
        user = request.user
    else:
        # Throw some raised exception here as the user is not valid...

    """The Game 1 page"""
    val1 = request.GET.get('ans1', '')
    res = "Incorrect"

    user_score = UserScore.objects.get_or_create(user=user)

    if val1 == 2:
        #user's score declared in model increase 5points
        #display correct and 5 points added to user
        res = "Correct"
        user_score.score += 5
        user_score.save()
    else:
        #user's score declared in model has no point
        #display incorrect and 0 point added to user
        res = "Incorrect"

    return render(request, 'morse_logs/game1.html', {'result': res})

Upvotes: 1

Mayur Fartade
Mayur Fartade

Reputation: 317

Change your views.py to

@login_required()
def game1(request):

    if request.user:
        user = request.user
    else:
        #throw exception

    """The Game 1 page"""
    val1 = request.GET.get('ans1', '')
    res = "Incorrect"

    if int(val1) == 2:
        #user's score declared in model increase 5points
        #display correct and 5 points added to user
        user_data = UserScore.objects.get(user=user)
        res = "Correct"
        user_data.score = int(user_data.score) + 5
        user_data.save()
    #no need to write else part  

    return render(request, 'morse_logs/game1.html', {'result': res})

Upvotes: 0

Related Questions