Answer the question
In order to leave comments, you need to log in
How to get get parameter in _clean form method?
Maybe I'm doing something fundamentally wrong.
Task: There is a tool. There are his movements between contractors. It is necessary to make a check that the move date is not earlier than the date of the last move.
class Tool(models.Model):
name = models.CharField(max_length=128, blank=False)
code_number = models.CharField(max_length=8, blank=True, null=True, unique=True)
buy_date = models.DateField(blank=True, null=True)
comment = models.TextField(blank=True)
class ToolMove(models.Model):
tool = models.ForeignKey(Tool, on_delete=models.CASCADE)
date = models.DateField(blank=True)
to_supplier = models.ForeignKey(Supplier, related_name='to_supplier', on_delete=models.PROTECT)
comment = models.TextField(blank=True)
path('toolmove/<int:tool_pk>/create/', ToolMoveCreateView.as_view(), name='toolmove_create_url'),
def post(self, request, tool_pk):
tool = get_object_or_404(Tool, pk=tool_pk)
bound_form = self.form_name(request.POST)
if bound_form.is_valid():
toolmove = ToolMove()
toolmove.tool = tool
toolmove.date = bound_form.cleaned_data['date']
toolmove.to_supplier = bound_form.cleaned_data['to_supplier']
toolmove.comment = bound_form.cleaned_data['comment']
toolmove.save()
if self.redirect_url:
return redirect(self.redirect_url)
else:
return redirect(tool)
action_url = reverse(self.action_url, kwargs={'tool_pk': tool_pk})
record_name = self.record_name
context = {
'form': bound_form,
'action_url': action_url,
'record_name': record_name,
'tool': tool
}
return render(request, self.template, context=context)
def clean_date(self):
new_date = self.cleaned_data['date']
last_date = ToolMove.objects.filter(tool=???).last()
if last_date.date > new_date:
raise ValidationError('Дата перемещения не может быть раньше последнего перемещения')
return new_date
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question