django - Django:在 UpdateView 中覆盖 get_queryset()
问题描述
“项目”模型
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
我的 ModelForm 类
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'))
我的 UpdateView 类
class ProjectUpdateView(LoginRequiredMixin, UpdateView):
model = Project
form_class = ProjectForm
success_url = reverse_lazy('dashboard')
目前,我的 ProjectUpdateView 类正在替换我的 ProjectForm 类上指示的十进制值。我应该通过表格上指示的数字来增加这些数值,而不是替换它们。据我了解,这可以通过覆盖我的 ProjectUpdateView 类中的 get_queryset() 来实现。我该如何实现呢?
解决方案
为了增加值,你替换值x
when ,你可以通过重写方法来做到这一点:x + F('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 += F('actual_design')
form.instance.actual_development += F('actual_development')
form.instance.actual_testing += F('actual_testing')
return super().form_valid(form)
推荐阅读
- android - 如何从 firebase 和 setText 检索数据到 TextView?
- sqlite - 如何将数据从树莓派发送到我的笔记本电脑?
- java - Oracle JDK11 (LTS) - 作为开发人员/测试人员,我可以免费从 Oracle 获得 LTS 更新吗?
- ramda.js - Ramda,使用或条件过滤
- loops - 我的 for 循环和条件返回混淆
- android - Android Monospace 不表现为固定间距
- android - 带有底部导航的导航组件
- javascript - 如何避免 JavaScript 中的多个 IF 语句?
- sql - SQL触发器-检查其他表,如果存在则更新否则插入
- php - 404 Not Found 这不是基于 applications_id 的重定向