Asserting sum of values in Django-admin -


I have a dynamic model for a sweater and want to enter the structure of the material used in the admin panel (Say: "100% wool" or "50% wool, 50% cotton" or "50% wool, 45% cotton, 5% acrylic").

I have this model:

  square sweater (models.Model): wool = model.tectorfield (max_length = 3, default = 100, verbose_name = "wool ( %) ") Cotton = Model.EaterField (max_long = 3, default = 0, verbose_name =" cotton (%) ") acryl = models.IntegerField (max_length = 3, default = 0, verbose_name =" acryl (%) ")  

How can I say that the sum of wool, cotton and acrylic values ​​should be 100 so that a user can not enter "100% wool, 100% cotton, 100% acetyl" ?

You should do this at least in two places. To ensure that you do not get incorrect data in the model and let a user know that this amount does not add up to 100%. The form below is used to check the yoga during cleaning:

  class sweaterform: "" "Form for adding and updating sweaters." "Def clean (self): cleaned_data = self.cleaned_data Wool = cleaned_data.get ('wool)' cotton = cleaned_data.get ('cotton') acryl = cleaned_data.get ('acryl') # Check that the textile structure will be up to 100% If not, then 'wool' itself is not 'in itself' and 'cotton' and not 'acrylic' in itself. _Rarars and (wool + Cotton + ACeral)! = 100: msg = u "structure 100% up to 100%! "Self._errors ['wool]] = error list ([msg]) Remove the field from the # clean field because it is no longer valid Dell [' wool '] returns the cleaned data class meta: Model = Sweater  

Hope that helps!


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -