Answer the question
In order to leave comments, you need to log in
How to call model method in view (django)?
Hello.
There is a model:
class Arsenal(models.Model):
question_main = models.TextField(blank=True, null=True, help_text="Главное описание вопроса", verbose_name="Описание вопроса")
question_answer1= models.CharField(max_length=65, help_text="Максимальная длина 65 символов", verbose_name="Ответ 1")
question_answer2= models.CharField(max_length=65, help_text="Максимальная длина 65 символов", verbose_name="Ответ 2")
question_answer3= models.CharField(max_length=65, help_text="Максимальная длина 65 символов", verbose_name="Ответ 3")
answer_number1 = models.IntegerField(default=0, help_text="Начинается с 0", verbose_name="Число 1 ответов")
answer_number2 = models.IntegerField(default=0, help_text="Начинается с 0", verbose_name="Число 2 ответов")
answer_number3 = models.IntegerField(default=0, help_text="Начинается с 0", verbose_name="Число 3 ответов")
def get_persent_answer1(self):
per1 = round(self.answer_number1 * 100 / (self.answer_number1 + self.answer_number2 + self.answer_number3), 1)
return per1
def get_persent_answer2(self):
per2 = round(self.answer_number2 * 100 / (self.answer_number1 + self.answer_number2 + self.answer_number3), 1)
return per2
def get_persent_answer3(self):
# percent3 = round((((self.answer_number1 + self.answer_number2 + self.answer_number3)/100)*self.answer_number3), 1)
per3 = round(self.answer_number3 * 100 / (self.answer_number1 + self.answer_number2 + self.answer_number3), 1)
return per3
def arsenal_votedone(request, add_id):
arsenal_item = Arsenal.objects.get(id = add_id)
ars1 = arsenal_item.get_persent_answer1 #собственно, он и не работает.
Answer the question
In order to leave comments, you need to log in
You have methods - respectively, and call them as methods
...
ars1 = arsenal_item.get_persent_answer1()
@property
def get_persent_answer1(self):
per1 = round(self.answer_number1 * 100 / (self.answer_number1 + self.answer_number2 + self.answer_number3), 1)
return per1
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question