0

In the admin, would it be possible to filter selection from a live input in a field:

class ReadtToday(models.Model):
    author =  models.ForeignKey(Authors)                           # field 1)
    book = models.OneToOneField(Books, related_name='bookofday')   # field 2)

Now in the admin for 1) and 2) got a list of all of them, how I should proceed to get this behavior:

select author from field 1) autoupdate field 2) list with only data from current selected author selected by field 1).

class BooksList(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(BooksList, self).get_queryset(request)           
        return qs.filter(HowCanIGetAuthor=from_input_of_field_1)

Is possbile to apply field 1) selection to filter fields 2) using raw_id_fields?

summer
  • 153
  • 5
  • It is possible using ajax request to custom view ... read this http://bit.ly/1EIa7P8 and this http://stackoverflow.com/a/19910749/3033586 – madzohan May 01 '15 at 11:40

1 Answers1

1

The most simple way is to implement author selection as a form with submit type GET and check the request.GET inside ModelAdmin class

<form method="GET">
   <select name="author">
        <option>An author</option>
   </select>
</form>

def get_queryset(self, request):
    author = request.GET['author']
    qs = super(BooksList, self).get_queryset(request)           
    return qs.filter(author=author)
kmmbvnr
  • 5,863
  • 4
  • 35
  • 44
  • thanks for the advice! where I can look to understand how to add those form properties to the admin form field? – summer May 01 '15 at 14:35
  • Check here http://stackoverflow.com/questions/4794528/extending-django-admin-templates-altering-change-list – kmmbvnr May 02 '15 at 00:49