python - Properly Defining Relationships in Django DB -


I am working on the Fitness Games web app. The idea is that there is a certain number of weeks in which each person must complete before appearing next week. Even my models.py schema for the app is:

Import the user from the import model from django.contrib.auth.models

  class WeekOne (models.Model): squat = models.PositiveIntegerField () lunges = models.PositiveIntegerField () skipStairs = models.BooleanField () stairDaysCount = models.PositiveSmallIntegerField () # set to true (squat == 1000), (lunges == 250), # (skipStairs it is true), and (stairDaysCount == 3) weekOneComplete = models.BooleanField () class UserProfile (models.Model): user = models.OneToOneField (user) weekOne = models.ForeignKey (Viagne)   

I lost some points. First of all, obviously I want every user to be able to track their progress and any other user can see it. What is the best way to do this one week a foreign ? If so, how does each user's data be used after defining this relationship? For example, if I want to create the addSquats function, will I do something like this:

  user = user profile () user.squats + = 5   

Or do I have to do some other magic to reach the squat field?

Second, every time a user changes their progress (i.e., adding squats or lunghs), I want to see if all the fields are from a certain benchmark. If they have, I want to set the weekOneComplete to the right. Each addition will be triggered by some javascript when a user clicks on a button - but where will I add the function that updates / updates the database? Firstly, obviously I want every user to be able to track their progress and to any other user. Not able to see.

In the template, you can do this

  User: {{user.username}} Squatt: {{user.userprofile.squats}} lunges : {{user.userprofile.lunges}} Skip Stairs: {if% user.userprofile.skipStairs%} Yes {else %%} not {% endif%} ladder: {{user.userprofile.stairDaysCount}}} weeks unscathed {% if user.userprofile.weekOneComplete%} Yes else {%%} progress - {% endif%}   

each user's data after defining the relationship How does it use? For example, if I want to create an addSquats function

To access user data,

  user = user profile (user = request.user) user WeekOne.squats + = 5 user.weekOne. Save () user.save ()   

Second, every time a user changes their progress (i.e., a slut adds or lengthened), I want to see Whether all the fields are from a certain benchmark. They have, so I want to set weekOneComplete to the truth.

Under your UserProfile model, generate update checks function.

  class UserProfile (models.Model): user = models.OneToOneField (user) weekOne = models.ForeignKey (WeekOne) def check_updates (self): check = WeekOne.object.get (ID = SelfkweekOne) If check.skipStairs and \ check.squats == 1000 and \ Check.lunges == 250 and \ check.stairDaysCount == 3: check.weekOneComplete = True check.save ()   

So every time you update the data for that user, just call this method:

  user = UserProfile.objects.get (user = request.user ) User.check_updates ()    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -