Django is slow with large base

Dear All,

When I put this modal form in my code (cf. template.html), it make django soooo slowly
This kind of modal form works fine with small base but not with a large base my ‘People_Main’ database is around 30 000 object.

models.py
class Peoples_Affaires_Clients(models.Model):
   affaire = models.ForeignKey(Affaires_Main, on_delete=models.SET_NULL, null=True)
   people = models.ForeignKey(Peoples_Main, on_delete=models.SET_NULL, null=True)

views.py
   Affaires_Clients_FormSet	= inlineformset_factory(Affaires_Main, Peoples_Affaires_Clients, can_delete=True, fields=('people', ))
   my_affaire_clients_formset_form = Affaires_Clients_FormSet(instance=my_affaire_main)

Template.html
<!-- Modal Infos -->
<form method="POST" enctype="multipart/form-data">
	{% csrf_token %}
	{{ my_affaire_clients_formset_form.management_form }}

	<div class="modal fade" id="ModalClients" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-info modal-xl" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title">Modifications - Clients</h4>
				</div>
				<div class="modal-body">
					<table class="table table-hover table-borderless table-sm" id="table">
						<thead>
							<tr>
								<th>People</th>
								<th>Nom</th>
								<th>Prenom</th>
							</tr>
						</thead>
						<tbody>
							{% for uform in my_affaire_clients_formset_form %}
								{{ uform.id }}
								<tr>
									<td>
										<div class="form-group input-group">
											{% render_field uform.people class="form-control" placeholder="[-- people --]" %}
										</div>
									</td>
								</tr>
							{% endfor %}
						</tbody>		
					</table>
				</div>
				<div class="modal-footer">
					<button class="btn btn-light" type="button" data-dismiss="modal">Close</button>
					<button class="btn btn-secondary" name="btn_affaire_clients" type="submit">Save</button>
				</div>
			</div>
		</div>
	</div>
</form>

If you have an idea :wink:
Thanks a lot

Thais

30k objects in a drop-down are going to be really slow. I would consider adding the ability to search for a list of people first to narrow it down. Even if you can filter down by the first letter or two, it might make it a bit more manageable.

Thanks Jeff for your help.
That what I espected …

So, what is this tips to associate to an object another one from a big list ?
Are you using jQuery for this ? But in jQuery you nedd to give the full list …

If any idea I take it …

Thais

Look at tools like select2. They work by allowing the user to type in part of the requested items, and passes that search term to the server for the server to provide a filtered list. (Using this in django requires a custom view to perform that filtering. See django-select2.)

1 Like