Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>First of all, obviously I want each user to be able to track their own progress and not have any other users be able to see it.</strong></p> <p>In the template, you can do like this</p> <pre><code> User: {{ user.username }} squats: {{ user.userprofile.squats }} lunges: {{ user.userprofile.lunges }} Skip Stairs: {% if user.userprofile.skipStairs %} Yes {% else %} No {% endif %} Stair: {{ user.userprofile.stairDaysCount }}} Week Completed: {% if user.userprofile.weekOneComplete %} Yes {% else %} In-Progress {% endif %} </code></pre> <p><strong>How does accessing each user's data work once this relationship is defined? For example, if I wanted to create an addSquats function.</strong></p> <p>To access user data,</p> <pre><code>user = UserProfile(user=request.user) user.weekOne.squats += 5 user.weekOne.save() user.save() </code></pre> <p><strong>Second, every time a user makes a change to their progress (i.e., adds a squat or lunge), I want to check if all of the fields have met a certain a benchmark. If they have, I want to set weekOneComplete to true.</strong></p> <p>Under your UserProfile model, create check updates function.</p> <pre><code>class UserProfile(models.Model): user = models.OneToOneField(User) weekOne = models.ForeignKey(WeekOne) def check_updates(self): check = WeekOne.object.get(id=self.weekOne) if check.skipStairs and \ check.squats == 1000 and \ check.lunges == 250 and \ check.stairDaysCount == 3: check.weekOneComplete = True check.save() </code></pre> <p>So every time you update the data for that user, just call the method like this:</p> <pre><code>user = UserProfile.objects.get(user=request.user) user.check_updates() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload