S
S
Sergey Nizhny Novgorod2016-07-19 11:57:41
Django
Sergey Nizhny Novgorod, 2016-07-19 11:57:41

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 ответов")

The model has three methods:
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

I'm trying to get these methods, something like this:
def arsenal_votedone(request, add_id):
    arsenal_item = Arsenal.objects.get(id = add_id)
    ars1 = arsenal_item.get_persent_answer1 #собственно, он и не работает.

...
Is there a way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-07-19
@Terras

You have methods - respectively, and call them as methods

...
ars1 = arsenal_item.get_persent_answer1()

well, either add the @property decorator
@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 question

Ask a Question

731 491 924 answers to any question