有没有一种方法可以在Django的属性实例中存储元组? [英] Is there a way to store a tuple in an attribute instance in Django?

查看:44
本文介绍了有没有一种方法可以在Django的属性实例中存储元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在Django的属性实例中存储元组吗?

I am wondering if it is possible to store a tuple in a attribute instance in Django?

类似的东西:

class MyModel(models.Model):

    my_tuple_field = TupleField()

,然后创建MyModel的实例,如下所示:

and then create an instance of MyModel as follow:

an_instance = MyModel.objects.create(my_tuple_field=('foo', 'bar'))

谢谢

推荐答案

As @p14z suggests, the best way to save my tuple was the ArrayField. For my example, I just wanted to save a Polygon extent, with this format:

(3.7739613717694787,50.31527681737183,4.726162032377,50.49743217278623)

from django.contrib.postgres.fields import ArrayField
my_tuple_field = ArrayField(models.FloatField(), size=4, null=True)

这篇关于有没有一种方法可以在Django的属性实例中存储元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆