Django form with ManyToMany field with 500,000 objects times out

前端 未结 4 2205
走了就别回头了
走了就别回头了 2021-02-13 05:03

Lets say for example I have a Model called \"Client\" and a model called \"PhoneNumbers\"

class PhoneNumbers(models.Model):
    number = forms.IntegerField()

cl         


        
4条回答
  •  伪装坚强ぢ
    2021-02-13 05:46

    You need to create a custom widget for this field that lets you autocomplete for the correct record. If you don't want to roll your own: http://django-autocomplete-light.readthedocs.io/

    I've used this for its generic relationship support, the M2M autocomplete looks pretty easy and intuitive as well. see video of use here: http://www.youtube.com/watch?v=fJIHiqWKUXI&feature=youtu.be

    After reading your comment about needing it outside the admin, I took another look at the django-autocomplete-light library. It provides widgets you can use outside the admin.

    from dal import autocomplete
    from django import forms
    
    class PersonForm(forms.ModelForm):
        class Meta:
            widgets = {
                'myformfield': autocomplete.ModelSelect2(
                    # ...
                ),
            }
    

提交回复
热议问题