django-rest-witchcraft icon indicating copy to clipboard operation
django-rest-witchcraft copied to clipboard

Data not being commited to database

Open jhonatanTeixeira opened this issue 5 years ago • 8 comments

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

jhonatanTeixeira avatar Jul 17 '19 20:07 jhonatanTeixeira

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.

shosca avatar Jul 17 '19 20:07 shosca

Whats the difference between this project and django-sorcery?

jhonatanTeixeira avatar Jul 17 '19 22:07 jhonatanTeixeira

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.

shosca avatar Jul 17 '19 22:07 shosca

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

miki725 avatar Jul 17 '19 22:07 miki725

@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.

shosca avatar Jul 17 '19 23:07 shosca

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! 🎉

miki725 avatar Jul 17 '19 23:07 miki725

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.

shosca avatar Jul 17 '19 23:07 shosca

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.

jhonatanTeixeira avatar Jul 18 '19 10:07 jhonatanTeixeira