python - Django @staticmethod sum of two fields -


I have a quick question that I am trying to add a field to a model which is the sum of 2 fields . For example:

  class MyModel (models.Model) fee = models I thought I could add a @staticmethod inside the model:  
  @ Staticmethod def fee_total (auto): Returns self.fi + self.fi_stuff   

but I think the model's "fee_total" Can not use using the field:

  model  

Cheers

Cheers

I think that you want to add a method to your model, so it Can the.

@staticmethod is a decorator that declares the method for class , then what is the difference?

Well, long story short, static methods do not have any special examples, give an example for the object class object, by me class

such as @property before mention of everyone A decorator has been used to make a method as a variable ... so you do not have to explicitly use ()

, you want to do this:

  class MyModel (models.Model) fee = models.DecimalField () fee_gst = models.DecimalField () @property def fee_total (auto): Return self.fee + self.fee_gst   

Although the document is a Take a long approach:

  class MyModel (models.Model) = fee model. DecimalField () fee_gst = models.DecimalField () def _fee_total (self): return self.fee + Self.fee_gst fee_total = property (_fee_total)   

Both methods are quite similar, Use as a small hand.

Hope it helps. / P>

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -