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

Data not being commited to database #48

Open
jhonatanTeixeira opened this issue Jul 17, 2019 · 8 comments
Open

Data not being commited to database #48

jhonatanTeixeira opened this issue Jul 17, 2019 · 8 comments

Comments

@jhonatanTeixeira
Copy link

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
Copy link
Owner

shosca commented Jul 17, 2019

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
Copy link
Author

Whats the difference between this project and django-sorcery?

@shosca
Copy link
Owner

shosca commented Jul 17, 2019

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
Copy link
Collaborator

miki725 commented Jul 17, 2019

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
Copy link
Owner

shosca commented Jul 17, 2019

@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
Copy link
Collaborator

miki725 commented Jul 17, 2019

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
Copy link
Owner

shosca commented Jul 17, 2019

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
Copy link
Author

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.

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

3 participants