Answer the question
In order to leave comments, you need to log in
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. 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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question