1

I've found there is a model framework for PostgreSQL:

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import column_property
from ..base import Base

class Staff(Base):
    __tablename__ = 'account'
    id = Column(Integer, primary_key=True)
    name = column_property(Column(String(30), nullable=False))
    surname = column_property(Column(String(30)))
    otch = column_property(Column(String(30), nullable=False))
    b_date = column_property(Column(String(30)))
    pol = column_property(Column(String(3), nullable=False))
    email = column_property(Column(String(100)))
    gruppa = column_property(Column(String(30), nullable=False))
    fak = column_property(Column(String(30)))
    profile = column_property(Column(String(30), nullable=False))

There is a view that through a serializater must give details:

if request.method == 'GET':
    session = request.sa_session
    h = session.query(Staff).all()
    for i in h:
        print i.name
    serializer = TaskSerializer(h, many=True)
    return Response(serializer.data)

Description TaskSerializer:

from db.admin.db import Staff
from rest_framework import serializers

class TaskSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Staff

But when you run, there is an error:

type object 'Staff' has no attribute '_meta'

The joke is that if the model is built through Model.model:

from django.db import models

class Staff(models.Model):
    name = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    otch = models.CharField(max_length=30)
    b_date = models.CharField(max_length=30)
    pol = models.CharField(max_length=3)
    email = models.CharField(max_length=100)
    gruppa = models.CharField(max_length=30)
    fak = models.CharField(max_length=30)
    profile = models.CharField(max_length=30)

it all works.

Is it possible to use SQLAlchemy model or only Django's models.Model?

1 Answer 1

1

The Django REST Framework only supports Django ORM with its ModelSerializers. Since you are using SQAlchemy, you will not be able to use any of that goodness.

DEPRECATED (see below for new suggestion) - There is a library which attempts to add support for SQAlchemy to DRF - DjangoREST Alchemy. Full disclosure - I know the author who is a coworker. It however only does the read-only operations, so it will not support all the features DRF supports for Django ORM. You are more than welcome though to open issues and/or contribute!

EDIT 04/19 - DjangoREST Alchemy is deprecated. Would recommend to take a look at either django-sorcery or django-rest-witchcraft. They achieve much better DRF integration while making it feel as if you are working with Django ORM.

Sign up to request clarification or add additional context in comments.

5 Comments

I so understand this library not относитя to rest in any way, and simply independent? whether SQlAlchemy can be transferred to Model somehow?
As far as I know, there is no way to transform SQLAlchemy model to Django ORM model and that would seem like a bad idea for other reasons (e.g. how both manage db sessions, etc). Instead of transforming models between 2 ORMs, it is better to create utilities to transform a model to a serializer directly.
how to transform model to the serializer?
Look at the library I linked to. It has some facilities to do that
you can write approximately as the serilizator will look, and I not absolutely understand that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.