[Django]-Django admin hangs (until timeout error) for a specific model when trying to edit/create

44👍

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?

👤spulec

10👍

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

6👍

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

0👍

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.

Leave a comment