24

I've been looking at the docs for search_fields in django admin in the attempt to allow searching of related fields.

So, here are some of my models.

# models.py
class Team(models.Model):
    name = models.CharField(max_length=255)


class AgeGroup(models.Model):
    group = models.CharField(max_length=255)


class Runner(models.Model):
    """
    Model for the runner holding a course record.
    """
    name = models.CharField(max_length=100)
    agegroup = models.ForeignKey(AgeGroup)
    team = models.ForeignKey(Team, blank=True, null=True)


class Result(models.Model):
    """
    Model for the results of records.
    """
    runner = models.ForeignKey(Runner)
    year = models.IntegerField(_("Year"))
    time = models.CharField(_("Time"), max_length=8)


class YearRecord(models.Model):
    """
    Model for storing the course records of a year.
    """
    result = models.ForeignKey(Result)
    year = models.IntegerField()

What I'd like is for the YearRecord admin to be able to search for the team which a runner belongs to. However as soon as I attempt to add the Runner FK relationship to the search fields I get an error on searches; TypeError: Related Field got invalid lookup: icontains

So, here is the admin setup where I'd like to be able to search through the relationships. I'm sure this matches the docs, but am I misunderstanding something here? Can this be resolved & the result__runner be extended to the team field of the Runner model?

# admin.py
class YearRecordAdmin(admin.ModelAdmin):
    model = YearRecord
    list_display = ('result', 'get_agegroup', 'get_team', 'year')
    search_fields = ['result__runner', 'year']

    def get_team(self, obj):
        return obj.result.runner.team
    get_team.short_description = _("Team")

    def get_agegroup(self, obj):
        return obj.result.runner.agegroup
    get_agegroup.short_description = _("Age group")
3
  • 4
    You should try 'result__runner__team__name'. Remember: you are passing a field to build a icontains lookup, so it can't be a ForeignKey. If it works, I'll post it as an answer.
    – Germano
    Jul 4, 2014 at 10:52
  • @Germano - You got it! I wasn't sure if there might be a default method to resolve a FK. Is there something you can add to return a field by default of a FK similar to that of a __str__() method on a model? Jul 4, 2014 at 11:05
  • Afaik there is no such a thing as a default model text search field. I'll post the answer so you can accept it.
    – Germano
    Jul 4, 2014 at 11:12

1 Answer 1

35

The documentation reads:

These fields should be some kind of text field, such as CharField or TextField.

so you should use 'result__runner__team__name'.

1
  • Thanks, same problem in django-extensions autocomplete
    – Moe Far
    Jun 23, 2016 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.