本文介绍了尝试编辑/创建时,特定模型的 Django 管理员挂起(直到超时错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个现在让我发疯了.以前没有发生过(因为客户需要,我什至得到了用户手册的屏幕截图).

This one is driving me nuts right now. It was not happening before (even got screenshots I had to do for the user-manual since the customer required it).

我首先在生产服务器上注意到它,然后我检查并在 Django 附带的开发服务器中也发生了.该模型出现在 django admin 的主页上,我可以单击它,它将显示销售点列表.每当我想编辑现有实例或创建新实例时,就会出现问题.

I first noticed it on production server and then I checked and also happens in the dev server that comes with Django. The model appears on the main-page of the django admin, I can click it and it will display the list of point of sales. The problem comes whenever I want to edit an existing instance or create a new one.

我只需单击链接(或将其放在栏上),它就会挂起.

I just click on the link (or put it on the bar) and it just hangs.

class PointOfSaleAdmin(admin.ModelAdmin):
    list_display = ('id','business', 'user', 'zipcode', 'address','date_registered')
    list_filter = ('business',)
    filter_horizontal = ('services',)
admin.site.register(models.PointOfSale, PointOfSaleAdmin)

这就是模型的注册.所有模型都在管理应用程序中注册,并且要测试的用户是超级用户.型号为:

That's the registration of the model. All models are registered in the admin application and the user to test this is a super user. The model is:

class PointOfSale(models.Model):
    user = models.ForeignKey(User)
    zipcode = models.ForeignKey(Zipcode)
    business = models.ForeignKey(Business)
    services = models.ManyToManyField(Service,
        verbose_name='available services')
    date_registered = models.DateField(auto_now_add=True)
    address = models.CharField(max_length=300)

加上一些不应该很重要的方法.另外,在此之前我上次测试管理员是在我创建所有这些方法之后,所以这不重要.

Plus a few methods that shouldn't really matter much. Plus, last time before this that I tested the admin was right after I created all those methods, so it shouldn't matter on this.

管理员很少需要访问此页面.通常它只是列出 PoS,但它仍然困扰着我.知道为什么它会被挂起吗?所有其他模型都运行良好.

The administrator very very rarely has to access this page. Usually it's just listing the PoS, but it still bothers me. Any idea of why it could be hanging? All other models are working just fine.

这发生在 Django 1.2.5 和 1.3 上

This is happening on both Django 1.2.5 and 1.3

我修改了超时限制.它正在工作,但不知何故,它需要几分钟才能真正发生.所以,背景中有一些东西需要很长时间.我不明白为什么它只发生在这个模型上,而且它发生在不同的环境(和小数据集)

I modified the timeout limits. It IS working, but somehow it takes several minutes for it to actually happen. So, there is something in the background that is taking ages. I don't understand how come it happens only for this model and it happens in different environments (and with small datasets)

我几乎想扇自己耳光.这么久没睡是我的错.

I almost feel like slapping myself. My fault for not sleeping for so long.

问题是邮政编码列表非常大(成千上万),并且外键字段作为 html 选择标记加载,这意味着它会加载每个条目.只是有多少数据是个问题.

The problem is that the zipcode list is pretty big (dozens of thousands) and the foreign key field is loaded as an html select tag, which means it loads every single entry. It's an issue with how much data there is simply.

现在我想知道如何控制外键在管理员中的显示方式.有人可以帮忙吗?

Now I wonder how to control the way the foreign key is displayed in the admin. Anyone could help with that?

推荐答案

在你的 admin.py 文件中,在相应的 admin 类下,设置

In your admin.py file, under the appropriate admin class, set

raw_id_fields = ('zipcode',)

这将显示邮政编码的 PK 而不是下拉列表.

This will display the zipcode's PK instead of a dropdown.

是否有理由将邮政编码设置为自己的模型,而不是使用 CharField 或实际邮政编码 模型字段?

Is there a reason that you are setting up zipcode as it's own model instead of using a CharField or an actual zipcode modelfield?

这篇关于尝试编辑/创建时,特定模型的 Django 管理员挂起(直到超时错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:47