Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: list_filter breaks on relational walk #85

Open
Zoidmania opened this issue Sep 29, 2016 · 1 comment
Open

Bug: list_filter breaks on relational walk #85

Zoidmania opened this issue Sep 29, 2016 · 1 comment

Comments

@Zoidmania
Copy link

Bug found in list_filter construct.
I can't use my specific application in this bug report b/c it is corporate knowledge, but I will do my best to describe it with an analogous app.

Say I have a BookCase model, and it can contain many Book models. There are many BookCases.
Book models point to a specific BookCase. I also have an Author model, which points to a specific Book (we assume an author only writes one book here). A Book may have multiple Authors.
In summary, the whole system may look something like this:

                BookCase1                         BookCase2
              /    |     \                        /        \
        Book1    Book2   Book3                 Book4       Book5
       /    |      |       \                /   |   \          \
Author1  Author2 Author3  Author4     Author5 Author6 Author7   Author8

Functions are in place to do the following:

from App.models import BookCase

# gets a specific book case
case = BookCase.objects.filter(identifier="whatever")[0]

# gets all the Books that point to this BookCase
case.books 

#--------------------------
from App.models import Book

# gets a specific book
book = Book.objects.filter(identifier="whatever")[0]

# gets the BookCase that this Book points to
case = book.case

# gets the Authors that point to this book
authors = book.authors

#--------------------------
from App.models import Author

# gets a specific Author
author = Author.objects.filter(identifier="whatever")[0]

# gets the gets the Book that this Author points to
author.book

# gets the BookCase that this Author's Book resides in
author.book__case

Now, if I want to make a model report using this tool to list all authors across all books across all book cases and filter by bookcase, I construct the following:

from App.models import BookCase, Book, Author
from model_report.report import reports, ReportAdmin
from django.utils.translation import ugettext

class AuthorData(ReportAdmin):
    title = ugettext('Book Summary')
    model = Book
    fields = [
        'book__case',
        'book__identifier',
        'book__title',
        'identifier',    # author ID in database
        'name',          # author name
        'address',       # author address
        'phone_number',  # author phone
    ]
    list_group_by = ( 'book__case',)
    list_filter = ('book__case',)  # HERE is the problem
    list_order_by = ('name',)
    type = 'report'
    exports = ('excel', 'pdf')
reports.register('AuthorData', AuthorData)

When rendering the page, I expect that filter would be a list of BookCases, but instead it is a list of Books. Furthermore, upon selecting a Book in that filter and clicking "Get results", an error is thrown saying there is no BookCase matching the filter value. I suspect this is because the parameter for that filter wants a BookCase object, but a Book is given instead, and there's a type mismatch.

@Zoidmania
Copy link
Author

Zoidmania commented Sep 29, 2016

For now, in my real project, I've simply added fields that reference the parents a few hierarchical steps up. It's a temporary solution for now, but not ideal; the database just got one column larger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant