Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Foreignkey within Inlineform

Hello I cant seem to filter a Foreignkey Dropdown within an 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:

class BookPrioInline(admin.TabularInline):
    model = BookPrio

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

admin.site.register(Author, AuthorAdmin)

I want the Books dropdown on the BookPrio inline to be filter on the selected Author in the admin panel. But can;t find out how to do it.

Some help would be welcome

like image 972
Stephan Avatar asked Mar 03 '11 19:03

Stephan


People also ask

What is model ForeignKey?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django. db. models. related module but is typically referenced from django.

What does On_delete models Cascade do?

1. CASCADE. When the on_delete argument is set to cascade then deleting the referenced object will have substantial effect on the referred objects. This means when the referenced object is deleted from the database then all the entries of the object will also be deleted from the entire database.


1 Answers

I'm a little confused by your question but found it interesting.

You want the author dropdown on the inlines to be the selected author -- so the inline will always only have 1 choice, the current author?

Well, normally you'd use formfield_for_foreignkey http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

But you have a special case where each inline depends on the object being edited.

I didn't see any easy ways to access the edited objects so I put the formfield_for_foreignkey definition in the change_view, and assigned the inlines from within the view function.

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)
like image 158
Yuji 'Tomita' Tomita Avatar answered Sep 28 '22 10:09

Yuji 'Tomita' Tomita