5
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'

在“Akas”表中,我有 2225188 条记录,因此“更改”视图需要很长时间才能加载此字段。解决此问题的解决方案是什么?我可以为 m2m 小部件进行分页吗?

任何人都可以帮助解决这个问题吗?在 admin.py 我使用 filter_horizo​​ntal = ['Akas',] 显示 Aka 表的波纹管的图像

4

1 回答 1

2

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.

于 2012-11-02T08:41:12.423 回答