pyorient + Django:我应该如何正确创建模型以便序列化程序可以访问它们?

pyorient + Django : How should I create a model properly so that serializers can access them?

settings.py

from pyorient.ogm import declarative
Node = declarative.declarative_node()
Relationship = declarative.declarative_relationship()

models/user.py

from pyorient.ogm.property import *
from my_proj.settings import Node


class User(Node):
    email = String(unique=True)
    password = String()

serializers/register_serializer.py

from rest_framework import serializers, validators
from users.models.user import User


class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'password')

我得到以下异常

AttributeError: type object 'User' has no attribute '_meta

将 属性 添加到您的模型中,

class User(Node):
    email = String(unique=True)
    password = String()

    <b>@property
    def my_property(self):
        return "my property data"</b>

然后用serializers.Serializer代替serializers.ModelSerializer

class RegisterSerializer(serializers.Serializer):
    email = serializers.EmailField()
    password = serializers.CharField()
    my_property = serializers.CharField()


然后,您将获得序列化数据,

user_obj = User.object.get(id=1)
reg_serializer = RegisterSerializer(user_obj)
serialized_data = reg_serializer.data