[Answer]-Wrapping My Head Around Foreign Keys

1👍

Everywhere you refer to self.WeekOne I think you are meaning to refer to self.weekOne

weekOne is the instance variable. UserProfile instances have no such attributes WeekOne.

Standard Python naming conventions would suggest that you use week_one as the attribute name. Rewriting checkUpdates for you…

class UserProfile(models.Model):

    user = models.OneToOneField(User)
    week_one = models.ForeignKey(WeekOne)

    def check_updates(self):
        if (self.week_one.squats >= WeekOne.squatBenchmark and
            self.week_one.lunges >= WeekOne.lungBenchmark and
            self.week_one.stairs_day_count >= WeekOne.stair_days_count_benchmark and
            self.week_one.skip_stairs):
                self.week_one.week_one_complete = True
                self.save()
                self.week_one.save()

In your view code you are creating a UserProfile object but you are never saving it. You probably don’t want to create a new user profile everytime a request comes in though. Django has some builtin mechanisms for creating a userprofile automatically. Also, you are never creating a WeekOne instance. Something like

profile = UserProfile(user=request.user)
week_one = WeekOne()
week_one.save()
profile.week_one = week_one
profile.save()

There you are creating a profile for a user, creating the instance of WeekOne, adding it to the profile, and saving both of them to the database.

Leave a comment