samedi 9 août 2014

Python 2.7 - Django AdminModel inline provoque apache fonctionner à 100 % du CPU - Stack Overflow


I have these models in my django app:


class Route(models.Model):
pass
class Link(models.Model):
start = models.ForeignKey(Stop)
runtime = models.TimeField()
position = models.IntegerField()
route = models.ForeignKey(Route)

and these admin models:


class LinkInline(admin.TabularInline):
model = Link
class RouteAdmin(admin.ModelAdmin):
list_display = ('id', 'show_link_count')
inlines = [
LinkInline,
]

def queryset(self, request):
return Route.objects.annotate(link_count=Count('link'))

def show_link_count(self, inst):
return inst.link_count
show_link_count.short_description = "Stop count"

When I try looking at any specific Route (and see a list of Links associated with it) it causes the apache server to run 100% on one thread and steadily allocate more and more memory.


Am I doing something wrong here?


I am running Ubuntu 13.10 with django 1.6 and apache 2.4.6




I'm guessing you have a lot of records in the Stop model. A solution is to raw_id_fields to link inline. Try this:


class LinkInline(admin.TabularInline):
model = Link
raw_id_fields = ('start',)



As Bogdan said, you problem is probably caused by tons of record of Stop. Instead the way of using raw_id_fields, you may still want to explicitly view all your Stop instance in your Link inline. This can be achieved, but it is complicated.


Change your Inline class to


class LinkInline(admin.TabularInline):
model = Link
form = LinkForm

Then create Linkform


Class LinkForm(forms.Form):
start = Stop.objects.values_list('id', 'something')
#I don't believe you just have an empty class

def clean(self):
cleaned_data = super(LinkForm, self).clean()
cleaned_data['start'] = Stop.objects.get(id=cleaned_data['start'])
return cleaned_data

In this case, you will still load all the instances you have for you Stop model, but also give you a quick loading speed.


The code may be confused, let me know if you need more explanation on this.



I have these models in my django app:


class Route(models.Model):
pass
class Link(models.Model):
start = models.ForeignKey(Stop)
runtime = models.TimeField()
position = models.IntegerField()
route = models.ForeignKey(Route)

and these admin models:


class LinkInline(admin.TabularInline):
model = Link
class RouteAdmin(admin.ModelAdmin):
list_display = ('id', 'show_link_count')
inlines = [
LinkInline,
]

def queryset(self, request):
return Route.objects.annotate(link_count=Count('link'))

def show_link_count(self, inst):
return inst.link_count
show_link_count.short_description = "Stop count"

When I try looking at any specific Route (and see a list of Links associated with it) it causes the apache server to run 100% on one thread and steadily allocate more and more memory.


Am I doing something wrong here?


I am running Ubuntu 13.10 with django 1.6 and apache 2.4.6



I'm guessing you have a lot of records in the Stop model. A solution is to raw_id_fields to link inline. Try this:


class LinkInline(admin.TabularInline):
model = Link
raw_id_fields = ('start',)


As Bogdan said, you problem is probably caused by tons of record of Stop. Instead the way of using raw_id_fields, you may still want to explicitly view all your Stop instance in your Link inline. This can be achieved, but it is complicated.


Change your Inline class to


class LinkInline(admin.TabularInline):
model = Link
form = LinkForm

Then create Linkform


Class LinkForm(forms.Form):
start = Stop.objects.values_list('id', 'something')
#I don't believe you just have an empty class

def clean(self):
cleaned_data = super(LinkForm, self).clean()
cleaned_data['start'] = Stop.objects.get(id=cleaned_data['start'])
return cleaned_data

In this case, you will still load all the instances you have for you Stop model, but also give you a quick loading speed.


The code may be confused, let me know if you need more explanation on this.


0 commentaires:

Enregistrer un commentaire