从今天起,我的UpdateView不再起作用。每当我选择图标来编辑项目时,都会出现以下错误:

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

我以前从来没有在UpdateView中包含一个QuerySet,所以我不确定为什么现在要查询它。我对通用UpdateView的理解是内置了自我查询功能,但我可能是错的。

任何帮助将非常感激。

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'),

]

模板
{% 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 %}

最佳答案

解决方案:

解决它。原来我不小心从 View 中删除了model=Entry。将其添加回去后,它又开始工作。

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

    def form_valid(self, form):
        ....

10-08 00:38