Note that there are some explanatory texts on larger screens.

plurals
  1. POProperly Defining Relationships in Django DB
    text
    copied!<p>I'm working on a fitness game web app. The idea is that each week has a certain number of tasks that a person must complete before the next week is revealed. Here is my <code>models.py</code> schema for the app so far:</p> <p>from django.db import models from django.contrib.auth.models import User</p> <pre><code>class WeekOne(models.Model): squats = models.PositiveIntegerField() lunges = models.PositiveIntegerField() skipStairs = models.BooleanField() stairDaysCount = models.PositiveSmallIntegerField() # Set to true if (squats == 1000), (lunges == 250), # (skipStairs is True), and (stairDaysCount == 3) weekOneComplete = models.BooleanField() class UserProfile(models.Model): user = models.OneToOneField(User) weekOne = models.ForeignKey(WeekOne) </code></pre> <p>I'm lost on a few points. 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. Is making <code>weekOne</code> a <code>ForeignKey</code> the best way to do this? If so, how does accessing each user's data work once this relationship is defined? For example, if I wanted to create an <code>addSquats</code> function, would I do something like this:</p> <pre><code>user = UserProfile() user.squats += 5 </code></pre> <p>or would I have to do some other magic to get to the squats field? </p> <p>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 <code>weekOneComplete</code> to true. Each addition will be triggered by some javascript when a user clicks a button -- but where would I put the function that checks/updates the database? </p>
 

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