Model and views for my Project

I would need help creating models and views for my project. I need a model that will extend the auth.user model that I use to authorize users. There should also be three int columns, game1-score, game2-score, and game3-score, in this model, into which I will insert data using views. I need to make sure that the logged-in user is found in views and then find out if his record already exists in the created model and insert the score into the correct column using the value game_id, which also comes with a score to views, and if the column already has a value he has to compare it with the new one and save the bigger one.

I would really appreciate the help, especially with the views part, I have a model created somehow, but I don’t know if well, see below.

class UserGameScore(models.Model):
    user_rec = models.ForeignKey(User, on_delete=models.CASCADE)
    
    game1_score = models.IntegerField(null=True, blank=True)
    game2_score = models.IntegerField(null=True, blank=True)
    game3_score = models.IntegerField(null=True, blank=True)

What have you done so far with Django? Would you still consider yourself “new” to it?

If so, I always recommend people start out with either the Official Django Tutorial or the Django Girls Tutorial. Either one of them will help get you started with learning how to write views to update models.

Yes I am new in Django. I know little bit about basic functions. The main things that are problematic for me is comparing the original value and the new one. Other thinks are ok, I only want to write about whole situation. Can you help me with this.

I don’t understand what you’re asking for here. What are you trying to compare?

Again, can you be more specific about what you’re asking here?

You’re asking very broad questions, it’s going to be helpful if you were more specific about what you’re asking for.

ok, i need to find the row in the database of the user who is currently logged in and enter the value I get in the column. With the proviso that if there is already a value in that column, it will be compared with the new one and the larger one will be saved. It is already understandable.

You’ve identified some separate steps here:

  • Get the row of UserGameScore for the current user.
  • Enter a value somehow (still not clear what you’re envisioning here)
  • Compare the entered value to a value in that row.

Are you entering in 1 value or 3? If only 1 value, which of the three columns should it be compared against?
How are these values going to be entered?

Yes, your description is understandable, there’s just not enough detail or clarity there to provide guidance.

The principle is that I accept a request that is processed using a URL with predefined parameters, such as this one (/ <int: game_id> / <int: score>), which calls views. And in the views I have to recognize the logged in user and find his row in the database. I use game_id to recognize to which of these three columns I want to save. And if there is a value in this column, compare it with the score parameter and store a larger number in this column.

Cool!

Step 1: How would you define a view that accepts two parameters from a url? (I’m looking for the def statement for the view, not the path statement for the URL.
If necessary, see:

Step 2: How do you find the currently logged-on user in a view?
If necessary, see:

Step 3: How do you query a model for a specific instance?
See:

Step 4: How do you access fields within a model?
See: Writing your first Django app, part 2 | Django documentation | Django

Step 5: How do you compare two values in Python?
See: 4. More Control Flow Tools — Python 3.10.4 documentation

Step 6: How do you change a field within a model?
Again, see: Writing your first Django app, part 2 | Django documentation | Django

Once you can answer all six questions, you’ll have built your view.

2 Likes

What an elaborate answer. I dont know if it helps OP, but I have deep respect for all your hard work here on DF. Thank you Ken! :muscle:

Thank you, very much. I go through your steps, but I have one problem. I query ma database and want to compare two values, but I get error: ‘<’ not supported between instances of ‘DeferredAttribute’ and ‘int’

I’ll need you to post your view code - I can’t diagnose issues based on just a description. (In general this is going to mean that one of the two values you’re trying to compare is not the data type you think it is.)

Yes, the value from database have bad data type. But I don´t know what is DeferredAttribut. I think a have some mistake in step 4, because I don´t much understand.

Here is my views:

```
      def games_renderer(request, game_id, player_id, score):
if game_id ==1: 
    kod = UserGameScore.objects.get(pk=player_id)
    if UserGameScore.game1_score < score:
        UserGameScore.game1_score=score
        keyboard.press_and_release("Ctrl + w")
    
elif game_id ==2: 
    kod = UserGameScore.objects.get(pk=player_id)
    
    if UserGameScore.game2_score < score:
        UserGameScore.game2_score=score
        UserGameScore.save()
        keyboard.press_and_release("Ctrl + w")
     

elif game_id ==3: 
    one_entry = UserGameScore.objects.get(pk=player_id)
    if  UserGameScore.game3_score < score:
        UserGameScore.game3_score=score
        keyboard.press_and_release("Ctrl + w")

This retrieves an instance of UserGameScore - it’s this object (kod) that you want to compare, not the object definition.

1 Like