简体   繁体   中英

how to create a model for following requirements

I have profile model, it have multiple fields like passing, receiving, shooting. Then each fields have 4 section like(Right, Left, Top, Bottom). How to store the 4 different value for the every single field.

For your requirement you could use CharField with the different values as choices.

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. 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. 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

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