Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django prefetch when listing objects in admin

I have following models:

class UserProfile(models.Model):
  name = models.CharField()
  info = models.OneToOneField(UserInfo, related_name='user')

class UserInfo(models.Model):
  def __unicode__(self):
    return self.user.__unicode__() + self.age
  age = models.IntegerField()

class Ticket(models.Model):
  userinfo = models.ForeignKey(UserInfo)

The problem is now in the Ticket admin interface. Ticket's userinfo field is rendered as dropdown. If there is lots of UserInfo in the database, every one of them makes a single call to the database (UserProfile) to generate its name.

How can I tell the admin (or any widget) to prefetch related UserProfiles when it is rendering the UserInfo dropdown?

I am looking for something like

info = models.OneToOneField(UserInfo, related_name='user', prefetch_in_bulk=True)
like image 760
Juho Rutila Avatar asked Mar 22 '13 05:03

Juho Rutila


1 Answers

I figured out one way to do this:

class TicketAdminForm(forms.ModelForm):
  class Meta:
    model = Ticket
  userinfo = forms.ModelChoiceField(queryset=UserInfo.objects.prefetch_related('user').all())

class TicketAdmin(admin.ModelAdmin):
  form = TicketAdminForm

admin.site.register(Ticket, TicketAdmin)

Now the needed models are prefetched as a one sql query rather than many queries.

like image 86
Juho Rutila Avatar answered Oct 20 '22 16:10

Juho Rutila