[英]how to create a model for following requirements
I have profile model, it have multiple fields like passing, receiving, shooting.我有配置文件model,它有多个领域,如传球、接球、射门。 Then each fields have 4 section like(Right, Left, Top, Bottom).
然后每个字段都有 4 个部分,例如(右、左、上、下)。 How to store the 4 different value for the every single field.
如何为每个字段存储 4 个不同的值。
For your requirement you could use CharField
with the different values as choices.根据您的要求,您可以使用具有不同值的
CharField
作为选择。
Ex:前任:
class Profile(models.Model):
MY_CHOICES = (
('R', 'Right'),
('L', 'Left'),
('T', 'Top'),
('B', 'Bottom')
)
passing = models.CharField(max_length=1, choices=MY_CHOICES, default='R', blank=True)
receiving = models.CharField(max_length=1, choices=MY_CHOICES, default='R', blank=True)
shooting = models.CharField(max_length=1, choices=MY_CHOICES, default='R', blank=True)
It sounds like what you're describing is actually several related models.听起来您所描述的实际上是几个相关的模型。 Without the full details of what this data is representing, I have to guess at the most suitable names for those models - but let's say that 'passing', 'receiving' etc are skills, and you're measuring people's skill levels in those categories.
如果没有这些数据所代表的全部细节,我不得不猜测这些模型最合适的名称——但假设“通过”、“接收”等是技能,而你正在衡量人们在这些类别中的技能水平. The list of skills would then belong in a 'Skill' model - ie if you have 10 different skills, then this would be a table with 10 entries, one for each skill.
然后,技能列表将属于“技能”model - 即,如果您有 10 种不同的技能,那么这将是一个包含 10 个条目的表格,每个技能对应一个条目。 Then, whenever you store a person's results, you would store that as a set of linked records: one entry in the Person model, along with as many PersonSkillLevel records as you need to cover all of the skills.
然后,每当您存储一个人的结果时,您都会将其存储为一组链接记录:Person model 中的一个条目,以及涵盖所有技能所需的尽可能多的 PersonSkillLevel 记录。 Your models would look like this:
您的模型将如下所示:
class Skill(models.Model):
name = models.CharField(max_length=255)
class Person(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class PersonSkillLevel(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
skill = models.ForeignKey(Skill, on_delete=models.CASCADE)
right = models.IntegerField()
left = models.IntegerField()
top = models.IntegerField()
bottom = models.IntegerField()
And the data stored in these would be something like:存储在其中的数据将类似于:
Skill:技能:
id | name
---------------
1 | Passing
2 | Receiving
3 | Shooting
Person:人:
id | first_name | last_name
---------------------------
1 | Bobson | Dugnutt
2 | Todd | Bonzalez
PersonSkillLevel:人员技能等级:
id | person | skill | right | left | top | bottom
1 | 1 | 1 | 12 | 23 | 24 | 31 <- Bobson Dugnutt's scores for Passing
2 | 1 | 2 | 13 | 15 | 21 | 17 <- Bobson Dugnutt's scores for Receiving
3 | 2 | 1 | 25 | 14 | 20 | 12 <- Todd Bonzalez's scores for Passing
4 | 2 | 2 | 16 | 22 | 15 | 24 <- Todd Bonzalez's scores for Receiving
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.