S
S
Sergey Nizhny Novgorod2016-11-06 14:47:49
Django
Sergey Nizhny Novgorod, 2016-11-06 14:47:49

How to make 20 same model methods in Django?

Hello.
There is
a Model field:

point1 = models.IntegerField(default="0",
                                  help_text="1 - нет (красное), 2 - да (зеленое), 3 - нет (зеленое), 4 - да (красное)",
                                  verbose_name="Защита от спама")

There are 21 such lines (from point1 to point21)
Each line has a method:
def get_point1(self):
        if self.point1 == 1:
            return '<span class="chart_font_1"><i class="fa fa-times" aria-hidden="true"></i></span>'
        elif self.point1 == 2:
            return '<span class="chart_font_2"><i class="fa fa-check" aria-hidden="true"></i></span>'
        elif self.point1 == 3:
            return '<span class="chart_font_3"><i class="fa fa-times" aria-hidden="true"></i></span>'
        elif self.point1 == 4:
            return '<span class="chart_font_4"><i class="fa fa-check" aria-hidden="true"></i></span>'
        else:
            return '<span class="chart_font_1"><i class="fa fa-times" aria-hidden="true"></i></span>'

Questions:
1) Is there a way to set the fields point1 - point21 somehow cleverly, rather than just listing them?
1) Is there a way to set the get_point1 - get_point21 methods somehow cleverly, rather than just listing them?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Wolf, 2017-01-22
@likeapimp

So ?

E
entermix, 2017-01-22
@entermix

You can INSERT multiple records at the same time:
dev.mysql.com/doc/refman/5.5/en/insert.html

Post::insert(array(
  array(
  'title'  => 'Laravel - замечателен!',
  'author' => 'Джейсон',
  'body'   => 'Laravel очень удобен - используйте его, если вы ещё этого не делаете!'
  ),
  array(
  'title'  => 'Laravel - простая работа с БД!',
  'author' => 'Джейсон',
  'body'   => 'Нет ничего проще, чем работать с базами данных с помощью Eloquent.'
  )
));

https://laravel.com/posts/5

U
UksusoFF, 2017-01-22
@UksusoFF

https://laravel.com/docs/5.3/queries#chunking-results

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question