Django:在 UpdateView 中覆盖 get_queryset()

Django: Override get_queryset() in UpdateView

"Project" 型号

class Project(models.Model):
    company = models.ForeignKey('projects.Company', on_delete=models.PROTECT, related_name='projects')

    title = models.CharField('Project title', max_length=128)
    start_date = models.DateField('Project start date', blank=True, null=True)
    end_date = models.DateField('Project end date', blank=True, null=True)

    estimated_design = models.DecimalField('Estimated design hours', max_digits=5, decimal_places=1,
                                           validators=[MinValueValidator(Decimal('0.01'))])
    actual_design = models.DecimalField('Actual design hours', default=0, decimal_places=1, max_digits=5,
                                        validators=[MinValueValidator(Decimal('0.01'))])

    estimated_development = models.DecimalField('Estimated development hours', max_digits=5, decimal_places=1,
                                                validators=[MinValueValidator(Decimal('0.01'))])
    actual_development = models.DecimalField('Actual development hours', default=0, decimal_places=1, max_digits=5,
                                             validators=[MinValueValidator(Decimal('0.01'))])

    estimated_testing = models.DecimalField('Estimated testing hours', max_digits=5, decimal_places=1,
                                            validators=[MinValueValidator(Decimal('0.01'))])
    actual_testing = models.DecimalField('Actual testing hours', default=0, decimal_places=1, max_digits=5,
                                         validators=[MinValueValidator(Decimal('0.01'))])

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('project-update', kwargs={'pk': self.pk, 'slug': slugify(self.title)})

    @property
    def has_ended(self):
        return self.end_date is not None and self.end_date < timezone.now().date()

    @property
    def total_estimated_hours(self):
        return self.estimated_design + self.estimated_development + self.estimated_testing

    @property
    def total_actual_hours(self):
        return self.actual_design + self.actual_development + self.actual_testing

    @property
    def is_over_budget(self):
        return self.total_actual_hours > self.total_estimated_hours

我的模型class

class ProjectForm(ModelForm):

    class Meta:
        model = Project
        fields = ['actual_design', 'actual_development', 'actual_testing']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.add_input(Submit('submit', 'UPDATE'))

我的更新视图class

class ProjectUpdateView(LoginRequiredMixin, UpdateView):
    model = Project
    form_class = ProjectForm
    success_url = reverse_lazy('dashboard')

此刻,我的 ProjectUpdateView class 正在替换我的 ProjectForm class 上指示的十进制值。我应该按照表格上指示的数字递增这些数值,而不是替换它们。据我所知,这可以通过覆盖我的 ProjectUpdateView class 中的 get_queryset() 来实现。我究竟该如何实施?

为了增加值,当 <i>x</i> + F 时替换值 <em><code>x ('field_name'),你可以通过覆盖form_valid方法来实现:

from django.db.models import F

class ProjectUpdateView(LoginRequiredMixin, UpdateView):
    model = Project
    form_class = ProjectForm
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        form.instance.actual_design += <b>F('actual_design')</b>
        form.instance.actual_development += <b>F('actual_development')</b>
        form.instance.actual_testing += <b>F('actual_testing')</b>
        return super().form_valid(form)