Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 2225188 record So The Change view takes long time to load this field. What is the solution to resolve this issue ? Can i do pagination for the m2m widget ?

Can any one help for this issue ? In admin.py i am using filter_horizontal = ['Akas',] Image showing Bellow for the Aka Table

like image 779
Abhilash Joseph Avatar asked Nov 03 '22 12:11

Abhilash Joseph


1 Answers

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

You can create a custom widget on your own, with pagination. Although my suggestion is that you use an auto-complete widget. You can look for existing django packages here and use the one which suits your needs. The basic idea here would be to use ajax requests to dynamically get data from server instead of loading everything at once.

like image 101
Konstant Avatar answered Nov 09 '22 09:11

Konstant