B
B
Bjornie2017-08-16 17:21:16
Django
Bjornie, 2017-08-16 17:21:16

How to make a request from one model to another in ModelAdmin?

There are 2 models:

class Person:
  name = models.CharField()
  surname = models.CharField()
  some_id = models.CharField()

class Player:
  person = models.ForeignKey(Person)
  some_field = models.CharField()
In the admin, on the Records page for the Player, I want to search for Person (any of the specified fields). I found in the documentation that there is a get_search_results method for this, but it only works for its own model.
Here's a code example from the admin (slightly modified) that I want to set up to search for another model, not self.model.
class PlayerAdmin(admin.ModelAdmin):
    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(PlayerAdmin, self).get_search_results(request, queryset, search_term)
        try:
            print(search_term)
        except ValueError:
            pass
        else:
            queryset |= Person.objects.filter(some_id=search_term) # здесь указал другую модель
        return queryset, use_distinct

What I get: Cannot combine queries on two different base models.
Tell me how it's done? With OOP is not very strong yet. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question