How better to limit the number of major news?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
10 views
On the website there is a block with the main news, which can be no more than 3. How to carry out such a test: use a validator:
def validate_count_main_posts(): if Post.objects.filter(main_posts=True) >=3: raise ValidationError('"main posts Too much!")

or to perform this check in the save method of a model?

PS: In django happen to have a built-in feature I'm not aware of for such a function? (After all, the task is quite frequent)
PSS: I Understand that this may be a matter of taste, but damn perfectionism wants to find out the best way))
by | 10 views

2 Answers

0 like 0 dislike
Post.objects.filter(main_posts=True) [:3]
by
0 like 0 dislike
If I understand correctly that you want to display 3 news.
Can in templates( Where you will make a conclusion ) to write.

{% for post in post|slice:":3" %} // slice will bring 3 news. {% endfor %}
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,118 users