A
A
Andrew2020-04-15 15:41:00
Django
Andrew, 2020-04-15 15:41:00

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)


Adding movement is carried out from the tool card (DetailView). The query parameter is tool_pk.
path('toolmove/<int:tool_pk>/create/', ToolMoveCreateView.as_view(), name='toolmove_create_url'),

Since some of the fields are not taken from the form, I create the movement as follows (here I’m not very sure that the approach is correct, sniffing at the crooked code and a description of how to do it better is welcome :):
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)

Those. since not all attributes are entered in the form, I do this. Here it was probably possible to shove the tool into a hidden field and take everything from the form, but it happens that I get some of the attributes from the database or by calculation. Such an approach (when you do not use form.save(), but create an object and take some of the attributes from the form, and save some by the calculation method and from the database) has the right to life or should it be done differently?

I create a method to check the date:
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

And here, in order to get the latest date for the instrument, I need the pk of the instrument, which is passed in GET.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2020-04-15
@G_r_i_n_v_i_c_h

Another option is to push the tool_pk into the bound_form before calling is_valid(), but the option seems terrible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question