简体   繁体   中英

Verify my django model config

Just starting out with Django, however I think I've screwed up my model layout. I don't think I have a full understanding of relationships just yet.

Here is what I desire:

my model layout

Here is my model.py

edit: I modified it a bit as per below

from django.db import models

class User(models.Model):
        username = models.CharField(max_length=30, unique=True)
        password = models.CharField(max_length=100)
        birthday = models.DateField('birthday')
        email = models.EmailField(max_length=100)
        SEX = (
         ('M', 'Male'),
         ('F', 'Female'),
        )
        usersex = models.CharField(max_length=1, choices=SEX)
        height = models.CharField(max_length=3)
        createdat = models.DateField('join date')

        def __str__(self):
                return "%s %s %s %s %s %s" % (self.username, self.birthday, self.email, self.usersex, self.height, self.createdat)

class UserBench(models.Model):
        username = models.ForeignKey(User, related_name='bench', to_field='username')
        date = models.DateField()
        reps = models.IntegerField()
        weight = models.IntegerField()

        def __str__(self):
                return "%s %s %s %s" % (self.username, self.date, self.reps, self.weight)

class UserSquat(models.Model):
        username = models.ForeignKey(User, related_name='squat', to_field='username')
        date = models.DateField()
        reps = models.IntegerField()
        weight = models.IntegerField()

        def __str__(self):
                return "%s %s %s %s" % (self.username, self.date, self.reps, self.weight)

class UserDeadlift(models.Model):
        username = models.ForeignKey(User, related_name='deadlift', to_field='username')
        date = models.DateField()
        reps = models.IntegerField()
        weight = models.IntegerField()

        def __str__(self):
                return "%s %s %s %s" % (self.username, self.date, self.reps, self.weight)

class UserStats(models.Model):
        username = models.ForeignKey(User, related_name='stats', to_field='username')
        date = models.DateField()
        bodyweight = models.IntegerField()

        def __str__(self):
                return "%s %s %s %s" % (self.username, self.date, self.reps, self.weight)

Is this correct? I think I wanted to have a "ManyToManyField" instead of "ForeignKey".

Or, thinking about it, maybe I should have somehow added the foreign-key columns to the person? This is because each person will have 1 lift (but multiple entries in the database)...so that confuses me somewhat.

Also, what about my "to_field"? I think that's what I want to do, however I'm not sure if django handles that automatically and I've just screwed it up.

edit: I guess I am confused as to how to now join the data.

If I do:

 User.squat.all()
 AttributeError: 'ForeignRelatedObjectsDescriptor' object has no attribute 'all'

If I do:

 user = User.Objects.filter(username = 'name')
 user.deadlift.all()
 Traceback (most recent call last):
 File "<console>", line 1, in <module>
 AttributeError: 'QuerySet' object has no attribute 'deadlift'

If I do:

 * Login to admin interface
 * Go to UserSquat, and click "add user"
 * The username is now EVERY field from User, so for e.g. it's detecting the username as "name 1989-05-16 email@domain.com M 180 2015-11-17" (Which are the fields in User table)

picture for reference

So I've got myself very confused and broken everything ha. Thanks again.

Your use of ForeignKey is good! To make use of them, assign a related_name to each foreign key field, like so:

class UserSquat(models.Model):
        username = models.ForeignKey(User, related_name='squats')

That way, whenever you have a user, you can access all of their squat objects (?) via user.squats.all() .

If multiple users can share squats for some reason, ManyToManyField s are used. The format does not change.

class UserSquat(models.Model):
        # A squat can belong to more than one user
        username = models.ManyToManyField(User, related_name='squats')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM