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.
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.
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.
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.
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:
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")