Как связать представления с формами и сохранить это в db Django

У меня есть форма, которая пытается подключиться к нескольким представлениям, где я получаю множественный выбор формы ModelChoice, и я фактически не знаю, как сохранить их в db или подключить их к моей модели "WorkLog"

Я получил ошибку валидации, которую я вызвал в функции "create_worklog" с messages.error

Any advices pls?

models.py

class WorkLog(models.Model):
    worklog_date = models.DateField(verbose_name='Дата', )
    created_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)
    author = models.ForeignKey(User, blank=True, related_name='contractor_user',on_delete=models.CASCADE)

    contractor_counter = models.ForeignKey(CounterParty, blank=True, related_name='contractor_user', on_delete=models.CASCADE)

    contractor_object = models.ForeignKey(ObjectList, blank=True, related_name='contractor_user', on_delete=models.CASCADE)

    contractor_section = models.ManyToManyField(SectionList, default=None, related_name='contractor_user'
    description = models.TextField(blank=True,)

forms.py

class WorkLogForm(forms.ModelForm):
    worklog_date = forms.DateField(label='Дата', widget=forms.DateInput(
        attrs={'type': 'date', 'class': 'form-control', 'placeholder': 'Введите дату'}))

    description = forms.CharField(label='Описание',
                                  widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Описание'}))

    class Meta:
        model = WorkLog
        fields = (
            'worklog_date', 'contractor_counter', 'contractor_object', 'contractor_section', 'description'
        )

views.py

def object_list(request):
    get_counter = request.GET.get('counter')
    objects = ObjectList.objects.filter(contractor_guid__in=[get_counter])
    context = {'objects': objects, 'is_htmx': True}
    return render(request, 'partials/objects.html', context)


def sections_list(request):
    get_object = request.GET.get('objects')
    sections = SectionList.objects.filter(object=get_object)
    context = {'sections': sections}
    return render(request, 'partials/sections.html', context)


def create_worklog(request):
    counter_party = CounterParty.objects.filter(counter_user=request.user)
    if request.method == 'POST':
        form = WorkLogForm(request.POST)
        if form.is_valid():
            work_log = form.save(commit=False)
            work_log.author = request.user
            work_log.contractor_counter = counter_party
            work_log.contractor_object = object_list(request).objects
            work_log.contractor_section = sections_list(request).sections
            work_log = form.save()
            messages.success(request, 'Everything okay', {'work_log': work_log})
            return redirect('create_worklog')
        else:
            messages.error(request, 'Validation error')
            return redirect('create_worklog')
    else:
        form = WorkLogForm()
    context = {'counter_party': counter_party, 'form': form}
    return render(request, 'zesshi_test/home.html', context)

template.html

<form action="{% url 'create_worklog' %}" method="post">
    {% csrf_token %}

    <div class="container mt-5">

        <div class=" mt-4">
            {{ form.worklog_date.label_tag }}
            {{ form.worklog_date }}
        </div>
        <select class="form-select mt-4 mb-4"
                name="counter"
                hx-get="{% url 'objects' %}"
                hx-trigger="change"
                hx-target="#id_objects">

            <option selected>Open this select menu</option>
            {% for counter in counter_party %}
                <option value="{{ counter.pk }}">{{ counter.name }}</option>
            {% endfor %}
        </select>

        <div id="id_objects">
            {% include 'partials/objects.html' %}
        </div>

        <div id="id_sections" class="mt-4">
            {% include 'partials/sections.html' %}
        </div>
        <div class=" mt-3">
            {{ form.description.label_tag }}
            {{ form.description }}
        </div>

        <div class=" form-footer text-center">
            <button type="submit" class=" btn btn-primary w-50">Create WorkLog</button>
        </div>
    </div>

</form>
Вернуться на верх