I’m building a Django-based classifieds website.
Previously, my search form populated districts and categories using database queries like:
districts = (
AdPost.objects
.filter(admin_verified=True)
.values_list("district", flat=True)
.distinct()
)
To improve consistency and performance, I’ve now moved both district and category to static choices in the model.
Example:
class AdPost(models.Model):
CATEGORY_CHOICES = [
('fish', 'fish'),
('cow', 'cow'),
]
DISTRICT_CHOICES = [
('e', 'e'),
('j', 'j'),
]
category = models.CharField(max_length=50, choices=CATEGORY_CHOICES)
district = models.CharField(max_length=30, choices=DISTRICT_CHOICES)
In my view, I now pass these directly:
categories = AdPost.CATEGORY_CHOICES
districts = AdPost.DISTRICT_CHOICES
And render them in the search form:
{% for key, label in districts %}
{{ label }}
{% endfor %}
Question
Does using model choices like this add any noticeable server load on every page request, or are these choices loaded once and reused?
I want to confirm that this approach is safe and efficient compared to querying the database on each request.
What I expect (but want confirmation)
My understanding is that:
choices are static Python data
They are loaded once when Django starts
Rendering them in templates does not cause database queries
Is this correct?
Environment
Django 4.x
PostgreSQL / SQLite
Standard class-based views
Подробнее здесь: https://stackoverflow.com/questions/798 ... every-page
Увеличивает ли использование выбора модели Django для поисковых фильтров нагрузку на сервер при каждом запросе страницы? ⇐ Python
Программы на Python
1766305136
Anonymous
I’m building a Django-based classifieds website.
Previously, my search form populated districts and categories using database queries like:
districts = (
AdPost.objects
.filter(admin_verified=True)
.values_list("district", flat=True)
.distinct()
)
To improve consistency and performance, I’ve now moved both district and category to static choices in the model.
Example:
class AdPost(models.Model):
CATEGORY_CHOICES = [
('fish', 'fish'),
('cow', 'cow'),
]
DISTRICT_CHOICES = [
('e', 'e'),
('j', 'j'),
]
category = models.CharField(max_length=50, choices=CATEGORY_CHOICES)
district = models.CharField(max_length=30, choices=DISTRICT_CHOICES)
In my view, I now pass these directly:
categories = AdPost.CATEGORY_CHOICES
districts = AdPost.DISTRICT_CHOICES
And render them in the search form:
{% for key, label in districts %}
{{ label }}
{% endfor %}
Question
Does using model choices like this add any noticeable server load on every page request, or are these choices loaded once and reused?
I want to confirm that this approach is safe and efficient compared to querying the database on each request.
What I expect (but want confirmation)
My understanding is that:
choices are static Python data
They are loaded once when Django starts
Rendering them in templates does not cause database queries
Is this correct?
Environment
Django 4.x
PostgreSQL / SQLite
Standard class-based views
Подробнее здесь: [url]https://stackoverflow.com/questions/79852153/does-using-django-model-choices-for-search-filters-add-server-load-on-every-page[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия