Filter an external link inside a string

Hello, I can’t filter out the “Inside” popup menu in the Inline form.

These are my classes:

class Author(models.Model):
    name = models.CharField(max_length=50)
    desc = models.CharField(max_length=50)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title= models.CharField(max_length=50)

class BookPrio::
    author = models.ForeignKey(Author)
    book = models.ForeignKey(Book)
    prio = models.IntegerField()

my admin.py looks like this:

class BookPrioInline(admin.TabularInline):
    model = BookPrio

class AuthorAdmin(admin.ModelAdmin):
    inlines =(BookPrioInline,)

admin.site.register(Author, AuthorAdmin)

I want the Books on the BookPrio drop-down list to be the filter of the selected author in the admin panel. But you can’t find out how to do it.

Some help would be appreciated.

+5
source share
1 answer

I am a little confused by your question, but found interesting.

Do you want the drop-down list of the author on the built-in lines to be selected by the author - so that there will always be only one choice in the line, the current author?

, formfield_for_foreignkey http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

, .

, formfield_for_foreignkey change_view .

class BookPrioInline(admin.TabularInline):
    model = BookPrio

class AuthorAdmin(admin.ModelAdmin):
    inlines = (BookPrioInline,)

    def change_view(self, request, object_id, extra_context=None):
          def formfield_for_foreignkey(self, db_field, request, **kwargs):
              if db_field.name == 'book':
                  kwargs['queryset'] = Book.objects.filter(author__id=object_id)
              return super(ItemInline, self).formfield_for_foreignkey(db_field, request, **kwargs)

          ItemInline.formfield_for_foreignkey = formfield_for_foreignkey

          self.inline_instances = [ItemInline(self.model, self.admin_site)]

          return super(AuthorAdmin, self).change_view(request, object_id,
              extra_context=extra_context)


admin.site.register(Author, AuthorAdmin)
+7