简体   繁体   English

验证我的Django模型配置

[英]Verify my django model config

Just starting out with Django, however I think I've screwed up my model layout. 刚从Django开始,但是我认为我搞砸了我的模型布局。 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". 我想我想拥有一个“ ManyToManyField”而不是“ 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. 这是因为每个人都会有1个举升(但数据库中有多个条目)...所以使我有些困惑。

Also, what about my "to_field"? 另外,我的“ 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. 我想这就是我想要做的,但是我不确定django是否会自动处理它,而我只是搞砸了。

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! 您对ForeignKey使用很好! To make use of them, assign a related_name to each foreign key field, like so: 要使用它们,请为每个外键字段分配一个related_name ,如下所示:

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() . 这样,无论何时有用户,都可以通过user.squats.all()访问其所有下蹲对象(? user.squats.all()

If multiple users can share squats for some reason, ManyToManyField s are used. 如果多个用户由于某种原因可以共享蹲坐,则使用ManyToManyField 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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