question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CheckboxSelectMultiple widget setted up trough Meta class triggers a LOT of queries

See original GitHub issue

I’m on django 1.6 and testing with the lates dev branc of crispy forms (f75f4de128af36e174e4be9a7c201a9828dbf7aa) , and I have 2 models linked by a m2m relation, say Blogpost and Tags, I also have a BlogpostForm (modelform) that specifies tags as a field to display: If I specify CheckboxSelectMultiple as a widget for the tags field in the Meta class for BlogpostForm, and render that form trough crispy forms I end up with something like 120 queries for 20 tags, normal django form rendering does not have this problem (something like 6 queries in total). A workaround is to specify the widget not in the Meta class of BlogpostForm (widgets = {'tags' : CheckboxSelectMultiple,}) but in its __init__ method (self.fields['tags'].widget = CheckboxSelectMultiple()). Is that a plausible crispy-forms issue or I misunderstand how something related to that widget initialization works?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Krukovcommented, Jun 28, 2014

I had the same problem, But I found a simple solution that needs to be tested. Just add cache_choices=True to yours ModelMultipleChoiceField attrs. Be carefull, it is undocumented django ability that was introduced as a solution for this ticket https://code.djangoproject.com/ticket/3534

0reactions
carltongibsoncommented, Oct 17, 2017

This is a Django issue. It pops up everywhere we have a choices field.

I guess the solution is to make choices a callable and implement caching there.

If it’s an issue it’s a Django issue. I’m going to close this here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django: ModelMultipleChoiceField not working when Form's ...
I am trying to create an event staff management tool in Django. One part of the data model is that a staff member...
Read more >
Creating forms from models - Django documentation
The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute. Each...
Read more >
Django Forms for Many-to-Many Fields - Medium
Here, we set the members field to use the CheckboxSelectMultiple widget instead of the default multiple choice field.
Read more >
Using Django Form Fields, Widgets, and Attributes
These built-in widgets represent HTML elements. CharField() from django import forms # Create your forms here. class ExampleForm ...
Read more >
62 - Customizing Form Fields, Widgets, Placeholders, & CSS
62 - Customizing Form Fields, Widgets, Placeholders, & CSS - Python & Django 3.2 Tutorial SeriesTry Django 3.2 is a series to teach...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found