我有一个模型,根据对象是由用户还是由系统创建,对其名称字段使用不同的验证。
class Symbol(models.Model):
name = models.CharField(_('name'), unique=True, max_length=64)
creator = models.ForeignKey('User', null=True, on_delete=models.CASCADE)
def is_system_internal(self):
"""
whether or not this Symbol belongs to the system rather than having been created by a user
"""
return (self.creator is None)
def clean(self):
"""
ensure that the Symbol's name is valid
"""
if self.is_system_internal():
if not re.match("^_[a-zA-Z0-9\-_]+$", self.name):
raise ValidationError(
_("for system-internal symbols, the name must consist of letters, numbers, dashes (-) and underscores (_) and must begin with an underscore."),
params = { 'value' : self.name },
)
else:
if not re.match("^[a-zA-Z][a-zA-Z0-9\-_]*$", self.name):
raise ValidationError(
_("the symbol name must consist of letters, numbers, dashes (-) and underscores (_) and must begin with a letter."),
params = { 'value' : self.name },
)
我想创建一个Form和一个CreateView,用户可以使用它创建对象。当用户创建此类对象时,应将该用户用作“创建者”字段值的值。
目前它看起来像这样:
class SymbolCreateForm(forms.ModelForm):
name = forms.CharField(max_length=Symbol._meta.get_field('name').max_length, required=True)
class Meta:
model = Symbol
fields = ('name',)
class SymbolCreateView(LoginRequiredMixin, generic.CreateView):
form_class = SymbolCreateForm
template_name = 'main/symbol_create.html'
def form_valid(self, form):
# set the creator of the instance to the currently logged in user
form.instance.creator = self.request.user
return super(SymbolCreateView, self).form_valid(form)
我是这样写的,因为这是这个相关问题的答案:
Accessing request.user in class based generic view CreateView in order to set FK field in Django
不幸的是,它不起作用:View只允许我创建以下划线开头的符号,它应该相反。但是,当我创建符号时,无论如何都会正确设置创建者字段。
我认为问题在于,只有在clean()方法已经运行之后才会设置创建者字段。
如何在调用clean()方法之前设置创建者字段?
或者,有没有更好的方法来做我想做的事情? 在我看来,应该有一种更有效的方法来自动化逻辑,我有两个不同的名称字段验证器,其选择取决于创建者字段。
答案 0 :(得分:5)
您可以在instance.creator
方法中设置get_form_kwargs
。
class SymbolCreateView(LoginRequiredMixin, generic.CreateView):
form_class = SymbolCreateForm
def get_form_kwargs(self):
kwargs = super(SymbolCreateView, self).get_form_kwargs()
if kwargs['instance'] is None:
kwargs['instance'] = Symbol()
kwargs['instance'].creator = self.request.user
return kwargs
检查if kwargs['instance'] is None
表示该代码适用于CreateView
和UpdateView
。
答案 1 :(得分:0)
就我而言,我希望请求用户成为验证过程的一部分。
提议的解决方案here更合适。它建议使用form_valid
如下
def form_valid(self, form):
form.instance.author = self.request.user
# ... do some validation here about the user for example
if passed_validation:
return super().form_valid(form)
form.add_error(None, ValidationError({"author": "invalid author because ..."}))
return super().form_invalid(form)