尝试编辑/创建时,Django 管理员针对特定模型挂起(直到超时错误)

发布于 2024-11-04 02:07:23 字数 1341 浏览 3 评论 0原文

这个现在让我抓狂。以前没有发生过这种情况(甚至还得到了我必须为用户手册做的屏幕截图,因为客户需要它)。

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

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

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)

这就是模型的注册。所有模型都在管理应用程序中注册,测试的用户是超级用户。该模型是:

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)

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

管理员很少需要访问此页面。通常它只是列出 PoS,但这仍然困扰着我。知道为什么它会挂起来吗?所有其他型号都工作正常。

这发生在 Django 1.2.5 和 1.3

编辑:

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


我几乎想打自己一巴掌。怪我这么久没睡觉。

问题是邮政编码列表相当大(数十万),并且外键字段作为 html 选择标记加载,这意味着它加载每个条目。这只是有多少数据的问题。

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

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).

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.

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.

This is happening on both Django 1.2.5 and 1.3

EDIT:

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭水袖舞倾城 2024-11-11 02:07:25

我只是想补充一点,这里的另一个选项是创建一个 read_only_fields 列表。如果与具有大量选择的模型存在关系(在我的情况下,大量用户和讨论线程之间的相关表编目标志),但您不需要编辑该字段。您可以将其添加到 read_only_fields 列表中,这样只会打印值而不是选项。

class FlaggedCommentsAdmin(ModelAdmin):
    list_display = ('user', 'discussion', 'flagged_on')
    readonly_fields = ('user', 'discussion')

I just wanted to add that another option here is creating a read_only_fields list. In cases where there is a relationship to a model with a large number of choices(in my case a rel table cataloging flags between a large number of users and discussion threads) but you don't need to edit the field. You can add it to the read_only_fields list will just print the value rather than the choices.

class FlaggedCommentsAdmin(ModelAdmin):
    list_display = ('user', 'discussion', 'flagged_on')
    readonly_fields = ('user', 'discussion')
╰◇生如夏花灿烂 2024-11-11 02:07:25

对于仍然登陆此页面的人:正如 Mamsaac 在他的原始帖子中指出的那样,超时发生是因为 django 尝试将 ForeignKey 的所有实例加载到 html-select 中。 Django 2 允许您添加 自动-完整字段,它允许您异步搜索ForeignKey来处理这个问题。在您的 admin.py 中执行以下操作:

from django.contrib import admin
from .models import Parent, Child

@admin.register(Parent)
class ParentAdmin(admin.ModelAdmin):
    # tell admin to autocomplete-select the "Parent"-field 'children'
    autocomplete_fields = ['children']

@admin.register(Child)
class ChildAdmin(admin.ModelAdmin):
    # when using an autocomplete to find a child, search in the field 'name'
    search_fields = ['name']      

For people still landing on this page: As Mamsaac points out in his original post, the timeout happens because django tries to load all instances of a ForeignKey into an html-select. Django 2 lets you add an auto-complete field which asynchronously lets you search for the ForeignKey to deal with this. In your admin.py do something like this:

from django.contrib import admin
from .models import Parent, Child

@admin.register(Parent)
class ParentAdmin(admin.ModelAdmin):
    # tell admin to autocomplete-select the "Parent"-field 'children'
    autocomplete_fields = ['children']

@admin.register(Child)
class ChildAdmin(admin.ModelAdmin):
    # when using an autocomplete to find a child, search in the field 'name'
    search_fields = ['name']      
情场扛把子 2024-11-11 02:07:25

您是否尝试过检查 apache 日志(如果您显然使用 apache)或任何其他 HTTP 服务器相关日志?这可能会让您知道从哪里开始。

这是唯一受影响的型号吗?您提到了模型上的方法。尝试注释掉这些方法并重试(包括 __unicode__ 方法),只是看看它们是否会以某种方式影响它。将所有内容减少到最低限度(显然尽可能多),尝试推断回归开始的位置。

当您请求此页面时,尝试监视服务器资源。 CPU 会急剧上升吗?网络 I/O 怎么样?可能是数据库问题(不知何故?)。

抱歉,这并不能真正回答您的问题,但这些是我尝试诊断问题的第一个调试技术。

Have you tried checking the apache logs (if you're using apache obviously) or any other HTTP server related logs? That might give you an idea of where to start.

That's the only model that is affected? You mentioned methods on the model. Try commenting out those methods and trying again (including the __unicode__ method), just to see if they somehow affect it. Reduce everything down to the bare minimum (as much as possible obviously), to try and deduce where the regression started.

Try to monitor server resources when you request this page. Does CPU spike dramatically? What about network I/O? Could be a database issue (somehow?).

Sorry this doesn't really answer your question, but those are the first debugging techniques that I'd attempt trying to diagnose the problem.

|煩躁 2024-11-11 02:07:24

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

raw_id_fields = ('zipcode',)

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

您将邮政编码设置为其自己的模型而不是使用 CharField 或实际邮政编码 是否有原因模型字段

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

raw_id_fields = ('zipcode',)

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文