django manytomany模型关系在对象创建时使管理员崩溃

时间:2018-11-11 21:35:35

标签: django postgresql django-models django-admin

我的postgres数据库中有一个Event对象,并创建了一个新的Collection对象,以通过ManyToMany字段关系按主题对事件进行分组:

class Collection(models.Model):
    event = models.ManyToManyField('Event', related_name='collections')
    name = models.CharField(blank=True, max_length=280)
    slug = AutoSlugField(populate_from='name')
    image = models.ImageField(upload_to='collection_images/', blank=True)
    description = models.TextField(blank=True, max_length=1000)
    theme = models.ManyToManyField('common.Tag', related_name='themes')
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(default=False)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('collection', args=[self.slug])

    def clean(self):
        # because of the way db saves M2M relations, collection doesn't have a
        # type at this time yet, so image inheritance is
        # called from the signal which is triggered when M2M is created
        # (that means if an image is later deleted, it won't inherit a new
        # one when collection is saved)
        if self.image:
            validate_hero_image(self.image, 'image')

    def save(self, *args, **kwargs):
        try:
            self.full_clean()
        except ValidationError as e:
            log.error('Collection validation error (name = %s): %s' % (self.name, e))
        return super(Collection, self).save(*args, **kwargs)

在我的管理员中,我这样定义和注册CollectionAdmin:

class CollectionAdmin(admin.ModelAdmin):
    model = Collection
    verbose_name = 'Collection'
    list_display = ( 'name', )

但是,如果我进入管理员并尝试创建集合"GET /admin/app/collection/add/" 200,则请求频繁超时,并且Event M2M关系对数据库的查询负载从日志记录来看似乎非常繁重。作为参考,数据库目前有约100,000个事件。有没有更好的方法来(重新)组织我的管理字段,所以我可以选择特定事件(按名称或ID)添加到Collection,而无需在加载该视图(或创建视图)时有效地请求所有事件的QuerySet通过外壳在数据库中)?谢谢

1 个答案:

答案 0 :(得分:1)

有多种方法可以实现此目的。例如,您可以override the form fields管理员使用并指定另一个要使用的小部件,例如NumberInput

您还可以将event模型字段添加到raw_id_fields attribute of ModelAdmin。这样,Django不会尝试创建完全填充的选择输入,但会为您提供一种在需要时手动搜索事件的方法:

class CollectionAdmin(admin.ModelAdmin):
    model = Collection
    verbose_name = 'Collection'
    list_display = ('name', )
    raw_id_fields = ('event', )