shosca / django-rest-witchcraft Goto Github PK
View Code? Open in Web Editor NEWDjango REST Framework integration with SQLAlchemy
Home Page: https://django-rest-witchcraft.readthedocs.io
License: MIT License
Django REST Framework integration with SQLAlchemy
Home Page: https://django-rest-witchcraft.readthedocs.io
License: MIT License
I have defined my models using rest_witchcraft and serialized them using ModelSerializer. I am trying to use viewsets.
As they have a different primary key then 'pk' they work fine on list(), but throw an error on retrieve().
I figured this was because of primary_key and provided a lookup_field.
# This is my serializer :
from rest_witchcraft import serializers
class CartItemSerializer(serializers.ModelSerializer):
class Meta:
model = CartItem
session = session
fields = '__all__'
# My Views:
from rest_witchcraft.viewsets import ModelViewSet
class CartViewSet(ModelViewSet):
queryset = Cart.query
serializer_class = CartSerializer
lookup_field = 'cart_id'
On running the app throgh lookup_field it gives me an error:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/rest_framework/relations.py", line 376, in to_representation
url = self.get_url(value, self.view_name, request, format)
File "/usr/local/lib/python3.6/dist-packages/rest_witchcraft/fields.py", line 41, in get_url
return super(UriField, self).get_url(obj, view_name, None, format)
File "/usr/local/lib/python3.6/dist-packages/rest_witchcraft/fields.py", line 29, in get_url
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/reverse.py", line 50, in reverse
url = _reverse(viewname, args, kwargs, request, format, **extra)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/reverse.py", line 63, in _reverse
url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "/usr/local/lib/python3.6/dist-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'cart-detail' with keyword arguments '{'pk': 5}' not found.2 pattern(s) tried: ['commerce/cart/(?P<cart_id>[^/.]+)\\.(?P<format>[a-z0-9]+)/?$', 'commerce/cart/(?P<cart_id>[^/.]+)/$']
Pardon me if something is wrong, I am creating an issue for the first time.
Because I could not find much of discussion about rest_witchcraft anywhere around the internet.
Thanks.
The DRF ModelSerializer build field signature is:
def build_field(self, field_name, info, model_class, nested_depth):
the current witchcraft implementation is:
def build_field(self, field_name, info, nested_depth):
This causes:
E TypeError: build_field() missing 1 required positional argument: 'nested_depth'
I'm subclassing ModelSerializer:
class HalModelSerializer(NestedFieldsSerializerMixin, serializers.ModelSerializer):
I believe the witchcraft ModelSerializer should keep the method signature, and of course serializers.py:327 should be adjusted accordingly
I have a class like this one:
from sqlalchemy import CHAR, Column, ForeignKey, String, TIMESTAMP, Table, text
from sqlalchemy.dialects.mysql import INTEGER, LONGBLOB, LONGTEXT, SMALLINT
class CSITECONTENT(Base):
__tablename__ = 'C_SITE_CONTENT'
CONTENT_KEY = Column(String(50), primary_key=True)
CONTENT_VAL = Column(LONGBLOB, nullable=False)
MIME_TYPE = Column(String(50), nullable=False)
CREATED_DATE = Column(TIMESTAMP, nullable=False, server_default=text("'0000-00-00 00:00:00'"))
MODIFIED_DATE = Column(TIMESTAMP, nullable=False, server_default=text("'0000-00-00 00:00:00'"))
the issue is with sqlalchemy.dialects.mysql.LONGBLOB fields I get this kind of tracebacks:
Internal Server Error: /CSITECONTENT/
Traceback (most recent call last):
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/mixins.py", line 43, in list
return self.get_paginated_response(serializer.data)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 757, in data
ret = super().data
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 261, in data
self._data = self.to_representation(self.instance)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 675, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 675, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django_restql/mixins.py", line 77, in to_representation
return super().to_representation(instance)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 511, in to_representation
for field in fields:
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 372, in _readable_fields
for field in self.fields.values():
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django_restql/mixins.py", line 230, in fields
return self.get_allowed_fields()
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django_restql/mixins.py", line 80, in get_allowed_fields
fields = super().fields
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_framework/serializers.py", line 360, in fields
for key, value in self.get_fields().items():
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_witchcraft/serializers.py", line 373, in get_fields
_fields[field_name] = self.build_field(source, info, self.model, depth)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_witchcraft/serializers.py", line 489, in build_field
return self.build_standard_field(field_name, prop)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_witchcraft/serializers.py", line 100, in build_standard_field
field_class = self.get_field_type(column_info)
File "/home/yves/.virtualenvs/pycugate/lib/python3.6/site-packages/rest_witchcraft/serializers.py", line 114, in get_field_type
"Could not figure out type for attribute '{}.{}'".format(self.model.__name__, column_info.property.key)
KeyError: "Could not figure out type for attribute 'CSITECONTENT.CONTENT_VAL'"
there seems to be an issue with LONGBLOB fields.
I got a problem using this implementation. All insert queries happens on the console correctly. However, the commit is not being called and data never gets flushed into the database.
Am i missing something?
# models.py
from .settings import DATABASES
import sqlalchemy as sa
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_mptt.mixins import BaseNestedSets
engine = sa.create_engine('sqlite:///%s' % DATABASES['default']['NAME'], echo=True)
session = sa.orm.scoped_session(sa.orm.sessionmaker(bind=engine))
Base = declarative_base()
Base.query = session.query_property()
class Endereco(Base, BaseNestedSets):
__tablename__ = 'endereco'
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
description = sa.Column(sa.String)
# serializers.py
from rest_witchcraft import serializers
from .models import Endereco, session
class EnderecoSerializer(serializers.ModelSerializer):
class Meta:
model = Endereco
session = session
fields = ['description', 'parent_id']
# views.py
from rest_witchcraft import viewsets
from .models import Endereco
from .serializers import EnderecoSerializer
class EnderecoViewSet(viewsets.ModelViewSet):
queryset = Endereco.query
serializer_class = EnderecoSerializer
#urls.py
from django.conf.urls import url
rom rest_witchcraft import routers
from .views import EnderecoViewSet
from rest_framework_swagger.views import get_swagger_view
from django.db import connection
router = routers.DefaultRouter()
router.register(r'enderecos', EnderecoViewSet)
schema_view = get_swagger_view(title='Pastebin API')
urlpatterns = [
url(r'^$', schema_view),
path('', include(router.urls)),
path('api/', include('rest_framework.urls', namespace='rest_framework'))
]
The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.
serializers.BaseSerializer function build_standard_field_kwargs() uses rest_framework.fields.NullBooleanField
According to the deprecation notice, NullBooleanField was removed in DRF Version 3.14.0
The NullBooleanField is deprecated and will be removed starting with 3.14. Instead use the BooleanField field and set allow_null=True which does the same thing.
since i updated django rest witchcraft to version 0.11 and sorcery to 0.11 im getting this error, i needed to rollback the versions
serializing sqlalchemy.Unicode column type causes
raise KeyError(
"Could not figure out type for attribute '{}.{}'".format(self.model.__name__, column_info.property.key)
)
Hi,
I haven't seen a mention about pagination.
Is it supported ? Is there a way to plug it in ?
Thanks
Hi all,
I'm struggling to understand the workflow of the documentation.
models.py
models, or is that inside serializers.py
?serializers.py
?I am struggling to just get to a point where I can setup a simple model and reflect that in django-rest-framework in sqlalchemy format.
Would someone mind please providing a general overview of the steps needed to use this module?
Thank you for your time.
I'm a little confused about the intended way to use allow_create
and allow_nested_updates
. From what I can understand allow_create
means that if there isn't a related record a blank model will be instantiated:
django-rest-witchcraft/rest_witchcraft/serializers.py
Lines 693 to 694 in f7de546
There seems to be an inconsistency in how allow_create
is used when the nested serializer is a ListSerializer (many=True) versus when it is a BaseSerializer.
As a ListSerializer setting allow_create=True
implies allow_nested_updates=True
because the related object will be treated the same way:
django-rest-witchcraft/rest_witchcraft/serializers.py
Lines 784 to 792 in f7de546
But as a BaseSerializer setting allow_create=True
alone will instantiate a blank model but will not update it unless you also set allow_nested_updates=True
:
django-rest-witchcraft/rest_witchcraft/serializers.py
Lines 763 to 776 in f7de546
Setting allow_create=True
alone will save the nested serializer with null for all values.
I can't tell if this difference in behavior is intentional or a bug. Can you clarify about how allow_create
is supposed to be used?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.