shosca / django-rest-witchcraft

Django REST Framework integration with SQLAlchemy
https://django-rest-witchcraft.readthedocs.io
MIT License
47 stars 11 forks source link

Data not being commited to database #48

Open jhonatanTeixeira opened 5 years ago

jhonatanTeixeira commented 5 years ago

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'))
]
shosca commented 5 years ago

Looks like you're missing the middleware to commit. You can use SQLAlchemyDBMiddleware from django-sorcery. Something simple like

class MyMiddleware(SQLAlchemyDBMiddleware):
    db = session

and register that in MIDDLEWARES django settings.

jhonatanTeixeira commented 5 years ago

Whats the difference between this project and django-sorcery?

shosca commented 5 years ago

django-sorcery does django and sqlalchemy integrations, django-rest-witchcraft does drf integrations. django-sorcery is a dependency of django-rest-witchcraft as they mostly deal with similar things, like mapping model metadata to a model form or model serializer.

miki725 commented 5 years ago

I would only add that you can use SQLAlchemyMiddleware for your middleware. no need to subclass anything then. that will commit across all your databases. its also mentioned in sorcery readme https://github.com/shosca/django-sorcery/blob/37087369bbc070cccf11e388e88a2c2387259a61/README.rst#L315

shosca commented 5 years ago

@miki725 he can't actually, he's not using databases and the django-sorcery config, he's got his own scoped session, which is fine as it is part of the use case. He just needs a middleware and that's the quickest way to get it.

miki725 commented 5 years ago

ah yes. didnt pay close attention to the actual code example. I guess this could a great opportunity to advertise sorcery and its magical abilities 😄 sorcerified db allows for use models somewhat django-like with things like Model.objects.filter(), etc. @jhonatanTeixeira check out the readme for more examples. and thanks for using the lib! 🎉

shosca commented 5 years ago

Yes!, you can declare sqlalchemy models with django-like syntax:

class Owner(db.Model):
    query_class = OwnerQuery

    id = db.IntegerField(autoincrement=True, primary_key=True)
    first_name = db.CharField(length=50)
    last_name = db.CharField(length=50)

see https://github.com/shosca/django-sorcery/blob/master/tests/testapp/models.py for more details.

jhonatanTeixeira commented 5 years ago

Very nice, maybe i will do a PR improvig the documentation after i get it all working, i like django and i like sqlalchemy. I think this lib is going to be perfect for my new architecture.