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

Question:

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?

Asked By: Mamsaac

||

Answers:

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.

Answered By: Josh Smeaton

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?

Answered By: spulec

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')
Answered By: nsfyn55

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']      
Answered By: Benjamin Maier
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.