【发布时间】:2015-04-16 12:11:48
【问题描述】:
在删除对象之前通过一些验证处理删除对象的最佳方法是什么?例如,在我的设置中有两个模型 - Game 和 Team(它们显然是相关的)。用户应该只能删除与任何游戏无关的团队。
我创建了一个用于删除团队的表单(没有任何字段)...
class TeamDeleteForm(ModelForm):
class Meta:
model = Team
fields = []
def clean(self):
# Check to see if this team is tied to any existing games
if self.instance.gameteams_set.exists():
raise ValidationError("This team is tied to 1 or more games")
return super().clean()
但后来我意识到基于类的视图 DeleteView 没有任何形式的 form_valid() 方法。我应该扩展通用 FormView 而不是 DeleteView 还是我缺少更好的方法?
【问题讨论】:
-
你看我下面的回答了吗?
-
@SimonCharette 我做到了,谢谢。但是,我认为我使用 FormView 有更好的解决方案。仍然不理想,在我发布我的解决方案之前,我正在等待看看是否有其他人会回答。
标签: django django-forms