Coder Social home page Coder Social logo

shosca / django-rest-witchcraft Goto Github PK

View Code? Open in Web Editor NEW
45.0 45.0 11.0 333 KB

Django REST Framework integration with SQLAlchemy

Home Page: https://django-rest-witchcraft.readthedocs.io

License: MIT License

Makefile 1.75% Python 97.54% Dockerfile 0.72%
django django-rest-framework django-sqlalchemy rest sqlalchemy

django-rest-witchcraft's People

Contributors

miki725 avatar pre-commit-ci[bot] avatar shosca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-rest-witchcraft's Issues

Could not resolve URL for hyperlinked relationship, Error while defining a lookup_field.

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.

build_field signature missing model_class

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

rest witchcraft doesn't seem to handle sqlalchemy.dialects.mysql.LONGBLOB fields

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.

Data not being commited to database

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'))
]

Initial Update

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.

NullBooleanField was removed in DRF Version 3.14.0

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.

pagination support ?

Hi,

I haven't seen a mention about pagination.
Is it supported ? Is there a way to plug it in ?

Thanks

I'm a little lost with the documentation

Hi all,

I'm struggling to understand the workflow of the documentation.

  1. Is the first model example the models.py models, or is that inside serializers.py?
  2. Do I import that model into the serializer?
  3. Do I import rest_framework serializers or the witchcraft serializers in 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.

Question about usage of allow_create and allow_nested_updates

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:

elif validated_data is not None and self.allow_create:
return self.model()

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:

for item in validated_data.get(field.source):
child_instance = field.child.get_object(item)
if child_instance and (field.child.allow_create or field.child.allow_nested_updates):
v = field.child.perform_update(child_instance, item, errors)
else:
v = child_instance
if v:
value.append(v)

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:

if field.source == "*":
value = validated_data
child_instance = instance
else:
if field.source not in validated_data:
continue
value = validated_data.get(field.source)
child_instance = getattr(instance, field.source, None)
child_instance = field.get_object(value, child_instance)
if child_instance and field.allow_nested_updates:
value = field.perform_update(child_instance, value, errors)
else:
value = child_instance

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.