Opened 8 years ago

Closed 8 years ago

#26825 closed Uncategorized (invalid)

MultipleObjectMixin and DB don't work together

Reported by: gilmrjc Owned by: nobody
Component: Uncategorized Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have the following CBV for my home page:

class HomeView(ListView):
    """Home view."""
    template_name = 'index.html'
    model = Post
    context_object_name = 'posts'
    posts_per_page, _ = Option.objects.get_or_create(name='posts_per_page',
                                                     defaults={'value': '5'}
                                                     )
    try:                                             
        paginate_by = int(posts_per_page.value)
    except ValueError:
        paginate_by = 5

But Django fails with the following error:

OperationalError: no such table: djangoapp_option

I can't do database migrations so this is a circular problem: Django complains because there isn't a database -> Database can't be migrated because Django complains -> Django complains . . .
The problem seems to be that Django "compiles" CVB before doing anything, before running commands. Is this bug or is a feature? Should I use function defined views for views that use database configurations?

Change History (1)

comment:1 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

You can't use module level queries, Option.objects.get_or_create(), in this case. See #26273 for more explanation.

In the future, please see TicketClosingReasons/UseSupportChannels for ways to get help with "is it a bug?" questions.

Note: See TracTickets for help on using tickets.
Back to Top