0

структура моделей следующая:

class AttachmentOwner(models.Model):
    attachment_owner_id = models.AutoField(primary_key=True)

class ProcessObjectOwner(models.Model):
    process_object_owner_id = models.AutoField(primary_key=True)

Document(AttachmentOwner, ProcessObjectOwner):
    ...

InternalDocument(Document):
    ...

view, в котором я пытаюсь создать объект InternalDocument:

InternalDocumentsUpdateModelView(UpdatePermissionMixin, UpdateModelView):
    ...
    def form_valid(self, form, **kwargs):
        ...
        status = int_status.under_consideration.name
        valid_addressees = [i for i in coaddressees.forms if not
        i.cleaned_data['DELETE']]
        user = get_delegate_or_user()
        delegation = get_current_delegation()
        draft = self.object
        for i, addressee in enumerate(valid_addressees):
            cleaned_addressee = addressee.cleaned_data['coaddressee']
            document = InternalDocument.objects.create(content=draft.content,
                                                  addressee=cleaned_addressee,
                                                        implementer=draft.implementer,
                                                               approved=draft.approved,
                                                               status=status,
                                                               nomenclature_of_deal=draft.nomenclature_of_deal,
                                                               text=draft.text,
                                                               addressee_subdivision=cleaned_addressee.subdivision,
                                                               content_type=ContentType.objects.get(
                                                                   app_label='documents',
                                                                   model='internaldocument'),
                                                               cipher=draft.cipher,
                                                               creator=user,
                                                               date_of_create=datetime.datetime.now(),
                                                               organization=draft.organization,
                                                               subdivision=draft.subdivision,
                                                               grant_to_all=draft.grant_to_all)

здесь coaddressees - формсет и тут на месте InternalDocument.objects.create происходит краш

Internal Server Error: /documents/internaldocuments/37/change/
Traceback (most recent call last):
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\important\work\edms-2\edms\src\edms\apps\documents\views\internaldocument.py", line 304, in post
    return self.form_valid(form, **data)
  File "C:\important\work\edms-2\edms\src\edms\apps\documents\views\internaldocument.py", line 356, in form_valid
    grant_to_all=draft.grant_to_all)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\base.py", line 568, in __init__
    _setattr(self, prop, kwargs[prop])
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 536, in __set__
    manager = self.__get__(instance)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 513, in __get__
    return self.related_manager_cls(instance)
  File "C:\Users\Sergey\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 830, in __init__
    (instance, self.pk_field_names[self.source_field_name]))
ValueError: "<InternalDocument: тест>" needs to have a value for field "attachmentowner_ptr" before this many-to-many relationship can be used.
[20/Jun/2019 12:56:08] "POST /documents/internaldocuments/37/change/ HTTP/1.1" 500 130603

1 ответ 1

0

Решено, не заметил что при создании было одно поле m2m.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.