V
V
Vampre2019-02-02 06:44:06
Django
Vampre, 2019-02-02 06:44:06

What is the best way to limit the number of top stories?

The site has a block with the main news, which can be no more than 3. What is the best way to do this kind of validation: use a validator:

def validate_count_main_posts():
    if Post.objects.filter(main_posts=True) >=3:
        raise ValidationError('"Too much main posts!")

or do this check in the model's save method?
PS: Is there by any chance a built-in feature in django that I don't know about for such a function? (After all, the task is quite frequent)
PSS: It is clear that this may be a matter of taste, but damn perfectionism wants to find out the best way))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LODIII, 2019-02-02
@LODIII

Post.objects.filter(main_posts=True) [:3]

B
Bogsan, 2019-02-02
@Bogsan

If I understand correctly, then you need to display 3 news.
You can write in templates (where you will draw a conclusion).

{% for post in post|slice:":3" %} // slice позволит вывести 3 новости.

{% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question