Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

django - Crazy number of fields

I want to record 144+ separate pieces of data for every record in my Django database. I think I know that I will need to create a field for each data point but it doesn’t seem very efficient. Is there a more effective way of doing this. It is a scoring system so I need to be able to enter values 1 to 10 as well as x, which counts as 10 and m, which counts as 0. The scores are recorded in groups of 3 or 6 values

Any thoughts

Amendment I have been chewing on this for days and it seems so simple when spoken out loud but I can't find any guidance on Google. I have decided to get the principle working on 6 scores but,ultimately it needs to be able to store 144 scores! My model looks like this:

class ScoreSheet(models.Model):
SCORE_ENTRY = (
    ('0', '0',),
    ('X','X',),
    ('10', '10',),
    ('9', '9',),
    ('8', '8',),
    ('7', '7',),
    ('6', '6',),
    ('5', '5',),
    ('4', '4',),
    ('3', '3',),
    ('2', '2',),
    ('1', '1',),
    ('M', 'M',),
)
Score_1 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY) 
Score_2 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY) 
Score_3 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY) 
Score_4 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY) 
Score_5 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY) 
Score_6 = models.CharField( max_length=2, default=0, blank=True, choices=SCORE_ENTRY)


def convertToNumber(self):
    if self.Score_1 == 'X':
        self.Score_1 = 10
    elif self.Score_1 == 'M':
        self.Score_1 = 0
    else: 
        self.Score_1 = int(self.Score_1)
    a = int(self.Score_1)
    if self.Score_2 == 'X':
        self.Score_2 = 10
    elif self.Score_2 == 'M':
        self.Score_2 = 0
    else: 
        self.Score_2 = int(self.Score_2)
    b = int(self.Score_2)
    if self.Score_3 == 'X':
        self.Score_3 = 10
    elif self.Score_3 == 'M':
        self.Score_3 = 0
    else: 
        self.Score_3 = int(self.Score_3)
    c = int(self.Score_3)
    if self.Score_4 == 'X':
        self.Score_4 = 10
    elif self.Score_4 == 'M':
        self.Score_4 = 0
    else: 
        self.Score_4 = int(self.Score_4)
    d = int(self.Score_4)
    if self.Score_5 == 'X':
        self.Score_5 = 10
    elif self.Score_5 == 'M':
        self.Score_5 = 0
    else: 
        self.Score_5 = int(self.Score_5)
    e = int(self.Score_5)
    if self.Score_6 == 'X':
        self.Score_6 = 10
    elif self.Score_6 == 'M':
        self.Score_6 = 0
    else: 
        self.Score_6 = int(self.Score_6)
    f = int(self.Score_6)
    return a + b + c + d + e + f
total = property(convertToNumber)

As you can see there is a lot of repetition in the code. This is not helped by the fact that the 10 score can be recorded in 2 ways, as a 10 and as an X which still counts as a 10 but, because it is nearer the centre of the target is used in a tie break situation. Misses are recorded as M but score 0. This is what the function convertToNumber does. This works but is not very elegant and will not scale well.

My second problem is how to count the number of X's in each end of 6 scores. I can get the view to return the scores as a dictionary or a list but cannot work out how to count the number of X's. I have tried the Count function and iterating over the list but neither seems to work. I cannot get the Count function to look at the key inside the 6 Score fields of a single record and identify similar values, it seems to want to count the single Key:value over many records. I have looked at placing any calculation in the model and the view with no luck.

I need to remove the repetition, if possible and calculate the number of X's.

Are there any hints and tips that anyone could offer?

question from:https://stackoverflow.com/questions/65907431/crazy-number-of-fields

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you considered using django-jsonfield.

To help separate your data into smaller fields.

Also, you can try separating your data into many models that have OneToOneField between them. and thus making your DB tables smaller.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...