Django infer a field from other fields

I am currently having a model:

class Current(models.Model):  
    field1 = models.IntegerField()  
    field2 = models.IntegerField()
    field3 = models.IntegerField()  

I need to have field3 to be set directly equal to field1 + field2 without actually sending it. What's the standard way in django to do this?

PS: Yes, I need to save field3 in the database alongwith the other fields.

Answers


Something like this? But not sure why this would need to be saved in the database.

class Current(models.Model):  
    field1 = models.IntegerField()  
    field2 = models.IntegerField()
    field3 = models.IntegerField()  

    def save(self, *args, **kwargs):
        self.field3 = self.field1 + self.field2
        super(Current, self).save(*args, **kwargs)

Need Your Help

Cakephp 3 find() within 2 hours

php datetime cakephp cakephp-3.0

This is pretty straight forward but I can't find it in the manual. I did this in cake 2 but can't find how to use the new ORM. I am doing a basic user changed password token. I want to check if the...

ImageAreaSelec: It wont preview the pre-selection on init

javascript jquery jquery-plugins methods imageareaselect

this is how I am preselecting (thanks to this question/answer ImageAreaSelect: preselect biggest thumbnail posible respecting aspectRatio)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.