Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-grappelli Autocomplete Lookups with multiple foreign key fields

I have a model with two fields that are foreign keys to other models.

class Homepage(models.Model):
  featured_user = models.ForeignKey('auth.user')
  featured_story = models.ForeignKey('site_stories.story')

  @staticmethod
  def autocomplete_search_fields():
     return ("featured_user__icontains", "featured_story__icontains",) # Is this right?


class HomepageAdmin(admin.ModelAdmin):
  raw_id_fields = ('featured_user', 'featured_story',)
  autocomplete_lookup_fields = {
    'fk': ['featured_user'],
    'fk': ['featured_story']  # <====== What should this be???
  }
admin.site.register(Homepage, HomepageAdmin)

After reading the admin docs and trying a few things, it became clear that you literally need to use the label "fk" for grappelli to apply the autocomplete lookup formatting to a field. So... how can I do this with this model, where there are multiple foreign key fields?

like image 681
TAH Avatar asked Oct 12 '12 04:10

TAH


1 Answers

class HomepageAdmin(admin.ModelAdmin):
   raw_id_fields = ('featured_user', 'featured_story',)
   autocomplete_lookup_fields = {
   'fk': ['featured_user','featured_story'],
   }
like image 50
Brenouchoa Avatar answered Sep 30 '22 16:09

Brenouchoa