# Loading big amount of options in django forms

**URL:** <https://forum.djangoproject.com/t/loading-big-amount-of-options-in-django-forms/29466>\
**Category:** Forms & APIs\
**Created:** [March 25, 2024, 4:46pm UTC](https://forum.djangoproject.com/t/loading-big-amount-of-options-in-django-forms/29466 "2024-03-25T16:46:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeanN00B](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jeann00b/32/19760_2.png) [@JeanN00B](https://forum.djangoproject.com/u/JeanN00B)\
**Post date:** [March 25, 2024, 4:46pm UTC](https://forum.djangoproject.com/t/loading-big-amount-of-options-in-django-forms/29466/1 "2024-03-25T16:46:49Z")

</div>

Hello! I am pretty new with Django, and now the problem I am facing is related to the long load times I am having when loading a form (maybe the solution to this could be applied not only to forms for sure) because I have a field that uses the [cities\_light](https://github.com/yourlabs/django-cities-light/tree/master) cities to save city / state / country where a person is located, this is to register clients in a DB, so the model.py I am using is something like this:

```auto
class Client(models.Model):
    id_number = models.CharField(max_length=20, primary_key=True, null=False)
# many other fields...
    res_city = models.ForeignKey('cities_light.City', on_delete=models.SET_NULL, null=True, blank=True, default=None)

```

and the form where I am calling it is like this:

```auto
class AddClientForm(forms.ModelForm):
    class Meta:
        model = Client
        exclude = ['salesman_refer']
        labels = {
        # some labels...
        }
    res_city = s2forms.ModelSelect2Widget()

    def __init__ (self, *args, **kwargs):
        super(AddClientForm, self). __init__ (*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            # a crispy forms layout
        )

```

the view that is calling this form is this:

```auto
@login_required 
def clients_create(request):
    if request.method == 'POST':
        form = AddClientForm(request.POST)
        if form.is_valid():
            User = get_user_model()
            client = form.save(commit=False)
            client.salesman_refer = User.objects.get(username=request.user.username).userprofile
            client.save()
            return redirect('/dashboard/clients/')            
        else:
            return render(request, 'clients/new_client.html',
                    {'form': form})
    else:
        form = AddClientForm()
        return render(request, 'clients/new_client.html',
                    {'form': form})

```

and finally the new\_client.html template where it is loading…

```auto
{% extends 'core/base.html' %}
{% load static %}

{% block title %} Nuevo Cliente {% endblock %}

{% block content %}
<div class = 'py-1 px-1'>
    {% load tailwind_filters %}
    {% load crispy_forms_tags %}
    <form method="post" action=".">
        {% csrf_token %}
        {% crispy form %}
        <!--{{ form|crispy }}-->
    </form>

</div>
{% endblock %}

{% block scripts %}
<script>
    $("#id_res_city").select2({
        closeOnSelect: true,
        minimumInputLength: 3,
        caches: true,
        maximumSelectionLength: 10,
    });
</script>
{% endblock %}

{% block stylesheets %}
<style>
    .select2-container{
        border: 1px solid #e2e8f0;
        border-radius: .5rem;
        line-height: 1.5;
        padding: .5rem 1rem .5rem 1rem;
        background-color: white;
        border-width: 1px;
    } 
    .select2-container--default .select2-selection--single{
        border: none;
    }

    body {
    --iuw-background: white !important;
    --iuw-border-color: rgb(209 213 219 ) !important;
    --iuw-color: #{$dashgray} !important;
    --iuw-placeholder-text-color: #{$dashgray} !important;
    --iuw-dropzone-background: white !important;
    --iuw-image-preview-border: rgb(209 213 219) !important;
    --iuw-image-preview-shadow: rgba(171, 170, 170, 0.3);
    --iuw-add-image-background: #{$dashlight} !important;
    --iuw-add-image-color: #{$dashgray} !important;
}

    .select2-selection__arrow {
        display: none;
    }
</style>
{% endblock %}

```

I am using selec2, I tried to use djangoselect2, but I couldn’t make it works, also I expected that the caches: true will keep the options in the cache memory to be used again after an user register, but it is not the case… probably I am not configuring well my cache in settings.py:

```auto

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': '0.0.0.0:11211',
    },
    # … default cache config and others
    "select2": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "127.0.0.1:6379/2",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

SELECT2_CACHE_BACKEND = "select2"

```

any suggestion and help wil be welcomed! Thanks ^^  
btw: any source to keep learning django (besides documentation) that could help me strength my basis and improve, will be awesome!
