简体   繁体   English

在 Django 模型中使用 Python 数据类

[英]Using Python Dataclass in Django Models

My goal is to create a struct-like object in Django PostgreSQL, such as:我的目标是在 Django PostgreSQL 中创建一个类似 struct 的对象,例如:

specs.coordinate.x
specs.coordinate.y
specs.coordinate.z

x , y , z should therefore be subclasses of coordinate. x , y , z因此应该是坐标的子类。 They should be me mutable as well, wherefore named tuples cannot be used.它们也应该是可变的,因此不能使用命名元组。

I have tried it using the new dataclass:我已经尝试使用新的数据类:

from django.db import models
from dataclasses import dataclass

class Specs(models.Model):
    name = models.CharField(max_length=80)
    age = models.IntegerField()

    @dataclass
    class coordinate:
        x: float
        y: float
        z: float

However the coordinate x,y,z items are not visible in pgAdmin:但是坐标 x,y,z项在 pgAdmin 中不可见:

在此处输入图片说明

What am I doing wrong?我究竟做错了什么? Is there any better approach for this than with dataclass?有没有比数据类更好的方法?

If your goal is simply to use the syntax: specs.coordinate.x in your datasheet you can simply use a OneToOne Relation.如果您的目标只是在数据表中使用语法: specs.coordinate.x ,您可以简单地使用OneToOne关系。

This also allows for the special case, that either all Coordinates values have to be set or none.这也允许特殊情况,即必须设置所有坐标值或不设置。

from django.db import models

class Coordinate(models.Model):
    x = models.FloatField()
    y = models.FloatField()
    z = models.FloatField()


class Specs(models.Model):
    name = models.CharField(max_length=80)
    age = models.IntegerField()
    coordinate = models.OneToOneField(
        Coordinate, 
        null=True, 
        on_delete=models.SET_NULL
    )

Now you can use Specs just like the a Dataclass.现在您可以像使用 Dataclass 一样使用 Specs。

Define DataClassField定义数据类字段

This solution uses dacite library to achieve support to nested dataclasses.该解决方案使用英安岩库来实现对嵌套数据类的支持。

from dacite import from_dict
from django.db import models
from dataclasses import dataclass, asdict
import json

"""Field that maps dataclass to django model fields."""
class DataClassField(models.CharField):
    description = "Map python dataclasses to model."

    def __init__(self, dataClass, *args, **kwargs):
        self.dataClass = dataClass
        if not kwargs['max_length'] 
            kwargs['max_length'] = 1024
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        kwargs['dataClass'] = self.dataClass
        return name, path, args, kwargs

    def from_db_value(self, value, expression, connection):
        if value is None:
            return value
        obj = json.loads(value)
        return from_dict(data_class=self.dataClass, data=obj)

    def to_python(self, value):
        if isinstance(value, self.dataClass):
            return value
        if value is None:
            return value
        obj = json.loads(value)
        return from_dict(data_class=self.dataClass, data=obj)

    def get_prep_value(self, value):
        if value is None:
            return value
        return json.dumps(asdict(value)

Example usage示例用法

@dataclass
class MyDataclass1:
    foo: str

@dataclass
class MyDataclass2:
    bar: MyDataclass1

class MyModel(models.Model)
    dc1 = DataClassField(dataClass=MyDataclass1, null=True)
    dc2 = DataClassField(dataClass=MyDataclass2, null=True)

instance = MyModel(dc1=MyDataclass1(foo="foo"))
instance.save()
instance.dc1.foo # Should return "foo"
instance.dc1 # Should return <MyDataclass1 .... at ....>

Update更新

To add support for custom datatypes (for example datetimes) you need to specify JSON decoding and encoding for these types as represented below:要添加对自定义数据类型(例如日期时间)的支持,您需要为这些类型指定 JSON 解码和编码,如下所示:

def decode_datetime(dt: datetime) -> str:
    return dt.strftime('%Y-%m-%dT%H:%M:%S%z')

def encode_datetime(dt: str) -> datetime:
    return datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S%z')
    
def default(o):
    if isinstance(o, (datetime.datetime)):
        return decode_datetime(o)
    else:
        print(o)

class DataclassField(models.CharField):
    ...
    def from_db_value(self, value, expression, connection):
        ...
        obj = json.loads(value)
        return from_dict(data_class=self.data_class, data=obj,
                         config=Config(type_hooks={datetime.datetime: encode_datetime}))

    def to_python(self, value):
        ...
        return from_dict(data_class=self.data_class, data=obj,
                         config=Config(type_hooks={datetime.datetime: encode_datetime}))

    def get_prep_value(self, value):
        ...
        return json.dumps(asdict(value), sort_keys=True, indent=1, default=default)

I would use django's built-in PointField .我会使用 django 的内置PointField It stores Point objects.它存储 Point 对象。

so you would use所以你会使用

from django.contrib.gis.db import models


class Specs(models.Model):
    coordinate = models.PointField

    

In django you could access it as you would with a normal django model field:在 django 中,您可以像使用普通 django 模型字段一样访问它:

spec.coordinate

And since is a Point object, you could also do:由于是Point对象,您还可以执行以下操作:

spec.coordinate.x
spec.coordinate.y
spec.coordinate.z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM