How can I do pagination for M2M Django

class Movie(models.Model): title= models.CharField(max_length=100,) votes=models.IntegerField(null=True) year=models.IntegerField(null=True) aspect_ration=models.CharField(max_length=50,) mpaa=models.CharField(max_length=200,) rating =models.CharField(max_length=20,) imdbid=models.CharField(max_length=50,) top_250_rank=models.IntegerField(null=True) cover_url=models.CharField(max_length=500,) plot_outline=models.TextField(blank=True, null=True) summary= models.TextField(blank=True, null=True) pub_date = models.DateTimeField(null=True, blank=True,default=datetime.today()) akas_id = models.ManyToManyField('Akas', verbose_name=u'Akas ID',related_name="Akas_M2M_Movie") class Akas(models.Model): name=models.CharField(max_length=500,) def __unicode__(self): return u'%s' % (self.name) class Meta: verbose_name = 'Other Movie Title' verbose_name_plural = 'Other Movie Titles' db_table = 'Akas' 

In the "Akas" table, I have an entry 2225188. Therefore, it takes a long time to view this field. What is the solution to this problem? Can I paginate the m2m widget?

Can anyone help to solve this problem? In admin.py, I use filter_horizontal = ['Akas',] Image showing Bellow for the Aka Table

+4
source share
1 answer

In this case, it is much better to use a custom widget than the default widget for many-to-many fields or even the Foreign Key fields. Since you have a huge number of Akas instances, the HTML page itself becomes very large and therefore takes time to load.

You can create your own widget yourself, paginated. Although my suggestion is that you use an autocomplete widget. You can find existing django packages here and use the one that suits your needs. The main idea here would be to use ajax requests to dynamically retrieve data from the server, and not to download everything at once.

+2
source

Source: https://habr.com/ru/post/1443600/


All Articles