简体   繁体   中英

Calculated django model field that depends on all the table

I have a model that looks like this:

class MyModel():
    total_time = IntegerField()
    total_score = IntegerField()

and I want to add a property or a field to this model which will be called rate and will be calculated according to those fields but on all the table, So if we have these models:

 1. total time - 1, total score - 3
 2. Total time - 2, total score - 2
 3. Total time - 3, total score - 1

My expectation is: if the rate is calculated by time , number 3 will be rated 1, but if it is calculated by score number one will be rated 1.

What is the best practice for such a case?

EDIT: To clear I want if now someone is entered like this: 4. total time - 4 total score - 4 He will be rated 1 in both but all the others will be rated according to the new rate

Thank in advance !!

You can calculate anything and override any field.


class MyModel():
    total_time = IntegerField
    total_score = IntegerField

    def save(self):
        # do calculations here
        # example: total_score = 3
        return super(MyModel, self).save()

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