How to avoid select lists on formsets and ManyToMany?

19 views
Skip to first unread message

Clive Bruton

unread,
Apr 1, 2021, 11:24:06 PM4/1/21
to django...@googlegroups.com
I have a ManyToMany model set, that on the edit uses a formset to add/
edit rows. I have this working, but the unfortunate part of it is
that the select that results from the ManyToMany relationship
populates with 16,000 records.

What I would like to do is, once the records are added, just have the
delete checkbox and show the 'description' field (uneditable) next to
it), and, rather than add records through a huge select list, add the
records by id.

To sketch it out, this is how it is:


<'Profile.directory-optin' checkbox>

<'Profile.directory-desc'>

<'Class.description' huge select>

<delete checkbox>

<!--hidden formset elements-->

<'Class.description' huge select>

<delete checkbox>

<!--hidden formset elements-->

<'Class.description' huge select>

<delete checkbox>

<!--hidden formset elements-->

<!--add new item-->

<'Class.description' huge select>

<delete checkbox>

<!--hidden formset elements-->

<save button>



And I'd rather it was something like:


<'Profile.directory-optin' checkbox>

<'Profile.directory-desc'>

<text of 'Class.description' field> <delete checkbox>

<!--hidden formset elements-->

<text of 'Class.description' field> <delete checkbox>

<!--hidden formset elements-->

<text of 'Class.description' field> <delete checkbox>

<!--hidden formset elements-->

<!--add new item-->

<new 'Class.id' text input field>

<!--does not need a 'delete' checkbox-->

<!--hidden formset elements-->

<save button>


The database tables look something like:


Profile Join table Class
======= ========== =====

id id id
directory-optin profile-id description
directory-desc class-id



Hope someone can help me out!


-- Clive

Ryan Nowakowski

unread,
Apr 1, 2021, 11:41:29 PM4/1/21
to django...@googlegroups.com
Django admin does something like this with raw_id_fields[1]. I think it works by just swapping out the form widget. But you could take a look at the admin code to see for sure.

[1] https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
Reply all
Reply to author
Forward
0 new messages