【发布时间】:2020-10-07 16:27:24
【问题描述】:
“项目”模型
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() 来实现。我该如何实现呢?
【问题讨论】:
标签: django django-views overriding modelform