Reputation: 11
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
?
Upvotes: 1
Views: 654
Reputation: 27861
The Django REST Framework only supports Django ORM with its ModelSerializer
s. 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.
Upvotes: 1