Import/startup performance improvements

This commit is contained in:
Raphael Michel
2020-06-30 11:34:34 +02:00
parent 5f50aa95eb
commit cf2af3c94d
8 changed files with 50 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
from django.core.cache import cache
from django.utils.translation import get_language
from django_countries import Countries
from django_countries.fields import CountryField
class CachedCountries(Countries):
@@ -27,3 +28,24 @@ class CachedCountries(Countries):
self._cached_lists[cache_key] = val
cache.set(cache_key, val, 3600 * 24 * 30)
yield from val
class FastCountryField(CountryField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("countries", CachedCountries)
super().__init__(*args, **kwargs)
def check(self, **kwargs):
# Disable _check_choices since it would require sorting all country names at every import of this field,
# which taskes 1-2 seconds
return [
*self._check_field_name(),
# *self._check_choices(),
*self._check_db_index(),
*self._check_null_allowed_for_primary_keys(),
*self._check_backend_specific_checks(**kwargs),
*self._check_validators(),
*self._check_deprecation_details(),
*self._check_multiple(),
*self._check_max_length_attribute(**kwargs),
]