Skip to content

Need a value for Id field when POSTing a new conversation

I am trying to POST a new conversation to my resource object and I am getting a 500 server error code.

I am posting the following:

curl 'http://localhost:8000/resources/1/conversations/' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: http://localhost:3000/mentor-dashboard/mentor-resource-detail/@http~@~_~_localhost~@8000~_resources~_1~_' -H 'Content-Type: application/ld+json' -H 'Origin: http://localhost:3000' -H 'Connection: keep-alive' -H 'Cookie: csrftoken=1PhZAHc4mH1kv6tXhLUlO7g8B000ZLg2r8Ezda3AIUDrWvT8TM0eLtJS4gWPBodc; sessionid=n1a8yiv2svsgf1vq4mqym7fsahtnen7s' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data '[{"@id":"./","http://happy-dev.fr/owl/#title":"Tata"}]'

Weird thing, it did work some time ago as I do have existing conversations on the same resource. When I do reply to an existing conversation the CURL request looks like the following and it works:

curl 'http://localhost:8000/conversations/3/message_set/' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Referer: http://localhost:3000/mentor-dashboard/mentor-resource-detail/@http~@~_~_localhost~@8000~_resources~_1~_' -H 'Content-Type: application/ld+json' -H 'Origin: http://localhost:3000' -H 'Connection: keep-alive' -H 'Cookie: csrftoken=1PhZAHc4mH1kv6tXhLUlO7g8B000ZLg2r8Ezda3AIUDrWvT8TM0eLtJS4gWPBodc; sessionid=n1a8yiv2svsgf1vq4mqym7fsahtnen7s' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data '[{"@id":"./","http://happy-dev.fr/owl/#text":"Toto"}]'

The main differences between the two calls are the POST target URLs:

  1. http://localhost:8000/resources/1/conversations/
  2. http://localhost:8000/conversations/3/message_set/

Because in both cases, the submitted object does not contain any ID:

  1. [{"@id":"./","http://happy-dev.fr/owl/#title":"Tata"}]
  2. [{"@id":"./","http://happy-dev.fr/owl/#text":"Toto"}]

In my models.py, the conversations are defined as follows:

conversations = models.ManyToManyField(Conversation, blank=True, related_name='resources')

Complete error message:

Traceback (most recent call last):
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/views.py", line 204, in dispatch
    response = super(LDPViewSet, self).dispatch(request, *args, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/views.py", line 142, in create
    self.perform_create(serializer)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/views.py", line 240, in perform_create
    super().perform_create(serializer, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/views.py", line 195, in perform_create
    serializer.save(**kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
    self.instance = self.create(validated_data)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/serializers.py", line 451, in create
    instance = self.internal_create(validated_data, model=self.Meta.model)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/djangoldp/serializers.py", line 489, in internal_create
    instance = model.objects.create(**validated_data)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
    obj = self.model(**kwargs)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/base.py", line 568, in __init__
    _setattr(self, prop, kwargs[prop])
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 536, in __set__
    manager = self.__get__(instance)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 513, in __get__
    return self.related_manager_cls(instance)
  File "/home/balessan/workspace/coopstarter/venv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 830, in __init__
    (instance, self.pk_field_names[self.source_field_name]))
ValueError: "<Conversation: Tata>" needs to have a value for field "id" before this many-to-many relationship can be used.
Edited by Benoit Alessandroni