Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdateView is missing a QuerySet Error

As of today, my UpdateView no longer works. Whenever I select the icon to edit an item I get the following error:

EntryUpdate is missing a QuerySet. Define EntryUpdate.model, EntryUpdate.queryset, or override EntryUpdate.get_queryset().

I have never had to have a QuerySet within my UpdateView before, so I am unsure why it is asking for one now. My understanding of the Generic UpdateView is that the self-query was built in, but I may be wrong.

Any help would be much appreciated.

views.py

class IndexView(generic.ListView):
    template_name = 'argent/index.html'

    # paginate_by = 10

    def get_queryset(self):
        return Entry.objects.all()

    def get_context_data(self, **kwargs):

        ctx = super(IndexView, self).get_context_data(**kwargs)

        # TODAY'S ENTRY
        ctx['entry_qs'] = Entry.objects.filter(date=today_date)

        # CURRENT SAVINGS TOTALS
        ctx['savings_qs'] = Savings.objects.filter(id=1)

        # MONTHLY TOTALS

        # November
        ctx['November16_qs'] = MonthYear.objects.filter(month='November')
        # December
        ctx['December16_qs'] = MonthYear.objects.filter(month='December')
        # January
        ctx['January17_qs'] = MonthYear.objects.filter(month='January')
        # February
        ctx['February17_qs'] = MonthYear.objects.filter(month='February')
        # March
        ctx['March17_qs'] = MonthYear.objects.filter(month='March')
        # # April
        # ctx['April_qs'] = MonthYear.objects.filter(month='April')
        # # May
        # ctx['May_qs'] = MonthYear.objects.filter(month='May')

        return ctx


class DetailView(generic.DetailView):
    model = Entry
    template_name = 'argent/detail.html'
    #
    # def get_context_data(self, **kwargs):
    #     ctx = super(DetailView, self).get_context_data(**kwargs)
    #     ctx['savings_qs'] = Savings.objects.filter(id=1)
    #     return ctx


class EntryCreate(CreateView):
    form_class = EntryForm
    template_name = 'argent/entry_form.html'

    def form_valid(self, form):
        if form.save(self):
            # total_euros_spent
            sum_euros = Entry.objects.aggregate(s=Sum('euros_sum')).get('s')
            sum_euros_f = "{0:.2f}".format(sum_euros)

            # total_dollars_spent
            sum_dollars = Entry.objects.aggregate(s=Sum('dollars_sum')).get('s')
            sum_dollars_f = "{0:.2f}".format(sum_dollars)

            # total_sum
            sum_savings = Entry.objects.aggregate(s=Sum('daily_savings_dollars')).get('s')
            sum_format = "{0:.2f}".format(sum_savings)

            # total_sum_format
            sum_abs_savings = Entry.objects.aggregate(s=Sum('daily_savings_dollars')).get('s')
            absolute = abs(sum_abs_savings)
            sum_abs = "{0:.2f}".format(absolute)

            Savings.objects.filter(id=1).update(total_savings=sum_format, total_savings_display=sum_abs,
                                                total_spent_dollars=sum_dollars_f, total_spent_euros=sum_euros_f)

            return super(EntryCreate, self).form_valid(form)

        else:
            return self


class EntryUpdate(UpdateView):
    form_class = EntryForm
    template_name = 'argent/entry_form.html'

    def form_valid(self, form):
        if form.save(self):
            # total_euros_spent
            sum_euros = Entry.objects.aggregate(s=Sum('euros_sum')).get('s')
            sum_euros_f = "{0:.2f}".format(sum_euros)

            # total_dollars_spent
            sum_dollars = Entry.objects.aggregate(s=Sum('dollars_sum')).get('s')
            sum_dollars_f = "{0:.2f}".format(sum_dollars)

            # total_sum
            sum_savings = Entry.objects.aggregate(s=Sum('daily_savings_dollars')).get('s')
            sum_format = "{0:.2f}".format(sum_savings)

            # total_sum_format
            sum_abs_savings = Entry.objects.aggregate(s=Sum('daily_savings_dollars')).get('s')
            absolute = abs(sum_abs_savings)
            sum_abs = "{0:.2f}".format(absolute)

            Savings.objects.filter(id=1).update(total_savings=sum_format, total_savings_display=sum_abs, total_spent_dollars=sum_dollars_f, total_spent_euros=sum_euros_f)

            return super(EntryUpdate, self).form_valid(form)

        else:
            return self

urs.py

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),

    url(r'entry/detail/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

    url(r'entry/add/$', views.EntryCreate.as_view(), name='entry-add'),

    url(r'entry/update/(?P<pk>[0-9]+)/$', views.EntryUpdate.as_view(), name='entry-update'),

]

template

{% if object_list %}
        {% for Entry in object_list %}
            <div class="col-md-3 col-sm-4 col-xs-6">
                <div class="thumbnail" style="background: #ebebeb" >
                    <h3 align="center" style="font-weight: bold; color: #337ab7;">{{ Entry.date }}</h3>
                    <div class="caption">
                        <h4 align="center" style="color: #FF5A09">€{{ Entry.euros_sum }}

                        <!-- View Details -->
                        <a href="{% url 'argent:detail' Entry.id %}"><button type="button" class="btn btn-link btn-lg">
                        <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
                        </button></a>

                        <!-- Update -->
                        <a href="{% url 'argent:entry-update' Entry.id %}"><button type="button" class="btn btn-link btn-lg" style="padding: 0">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                        </button>
                        </a>
                        </h4>
                    </div>
                </div>
            </div>
        {% endfor %}
        {% endif %}
like image 856
grigs Avatar asked Mar 27 '17 12:03

grigs


1 Answers

SOLUTION:

Fixed it. It turns out I accidentally deleted model=Entry from the view. Once I added it back, it started to work again.

class EntryUpdate(UpdateView):
    model = Entry
    form_class = EntryForm
    template_name = 'argent/entry_form.html'

    def form_valid(self, form):
        ....
like image 92
grigs Avatar answered Nov 16 '22 00:11

grigs