简体   繁体   English

我如何为M2M Django执行分页

[英]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. 在“ Akas”表中,我有2225188条记录,因此“更改”视图需要很长时间才能加载此字段。 What is the solution to resolve this issue ? 解决此问题的解决方案是什么? Can i do pagination for the m2m widget ? 我可以对M2m小部件进行分页吗?

Can any one help for this issue ? 有人可以帮忙解决此问题吗? In admin.py i am using filter_horizontal = ['Akas',] 在admin.py中,我正在使用filter_horizo​​ntal = ['Akas',] 该图显示了Aka表的波纹管

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. 由于您有大量的Akas实例,因此HTML页面本身变得非常大,因此加载时间很长。

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. 您可以在此处查找现有的django软件包然后使用适合您需求的软件包。 The basic idea here would be to use ajax requests to dynamically get data from server instead of loading everything at once. 这里的基本思想是使用ajax请求从服务器动态获取数据,而不是立即加载所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django rest framework:如何在id之外显示m2m和foreignkey字段? - django rest framework: How can I display m2m and foreignkey fields besides id? 如何使用 django-rest-framework 将嵌套的 m2m 字段序列化为 self ? - How can I serialize a nested m2m field to self with django-rest-framework? 如何在 Django 中创建正确的 m2m 信号以获取通知? - How can I create proper m2m signals in Django for get notifications? Django:如何验证M2M关系? - Django: how to validate m2m relationships? 如何从使用“通过”创建的 M2M 过渡到由 Django 管理的 M2M - How to transition from M2M created with 'through' to a M2M managed by Django Django —常见方案:具有M2M的产品->类别。 如何有效地查询产品查询集中的所有类别? - Django — Common Scenario: Product with M2M -> Categories. How do I efficiently query all categories on a product queryset? 如何从django rest框架类基于创建视图的用户个人资料上的M2M字段中添加对象? - How can I add an object to a M2M field on my users profile from django rest frameworks class based create view? 如何为现有的m2m字段创建自定义保存方法。 (Django的) - How do create a custom save method for an existing m2m field. (Django) 您如何用M2M字段的关联文件注释Django管理员的filter_horizo​​ntal? - How do you annotate Django admin's filter_horizontal with associated filed of the M2M field? 如何在 django 模板中反向查找 m2m 字段 - How to do a reverse look up of m2m field within django template
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM