Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend DjangoNode to be used in Django Restframework ModelSerializers #45

Open
contra-bit opened this issue Jan 15, 2021 · 2 comments
Open

Comments

@contra-bit
Copy link

Dear Devs,
I am working on building full support for django rest framework.
This includes a README over 1000 Lines with full examples on how to build a self documenting rest api specified with OpenAPI 3.0

Problem

Everything works perfect expect for the edge case that the parameters for POST request are separated with hyphen instead of underscores. Everything brakes if properties of a node are in hyphen case (test-name) instead of (test_name)

What i have tried

I have tried a few different approaches.

  • Using DjangoNode instead of structured Node
    According to drf you can then pass extra agrs, where the conversion happens. sadly because Django Node is not functionally equivalent to django.model this brakes
Traceback (most recent call last):
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 419, in run_validation
    value = self.to_internal_value(data)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 472, in to_internal_value
    for field in fields:
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 355, in _writable_fields
    for field in self.fields.values():
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 349, in fields
    for key, value in self.get_fields().items():
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 1028, in get_fields
    info = model_meta.get_field_info(model)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/utils/model_meta.py", line 35, in get_field_info
    opts = model._meta.concrete_model._meta
AttributeError: 'NoneType' object has no attribute '_meta'
[15/Jan/2021 23:06:42] "POST /mud/ HTTP/1.1" 500 20683

Traceback (most recent call last):
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/mixins.py", line 19, in create
    self.perform_create(serializer)
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/mixins.py", line 24, in perform_create
    serializer.save()
  File "/home/s0/Code/Learn/Neo4JProjects/neo4things/env/lib/python3.9/site-packages/rest_framework/serializers.py", line 205, in save
    self.instance = self.create(validated_data)

The issue here is that the conversion happens correctly but then i think python is confussed because there is a minus and property name is now an expression

I think the easiest would be to make DjangoNode work in the rest framework. What do you think. Any Ideas how I can get this to work?

If you are interested in my project write me, its going to be published under agpl soon

@contra-bit
Copy link
Author

My intuition tells me, that DjangoFields needs the function get_field info or simmilar

@whatSocks
Copy link
Collaborator

Hello @contra-bit this is interesting, we will check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants