Django 如何更改 IntegerRangeField 的边界值
Django how to change bounds value of IntegerRangeField
我有一个包含 IntegerRangeField 的模型,其中 returns 数据例如 NumericRange(1992, 1997, '[)') 将对象添加到数据库后。我需要范围的第二个选项也包含在其中,所以边界应该像 '[]'。
我在 psycopg2 documentation 中寻找解决方案,但不幸的是没有找到答案。
请帮忙!提前致谢。
我现在的models.py
from django.contrib.postgres.fields import IntegerRangeField
from django.contrib.postgres.validators import RangeMinValueValidator, RangeMaxValueValidator
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from datetime import datetime
class Bancnote(models.Model):
Dollar= 'Dollar'
Euro= 'Euro'
TYPE_CHOICES = (
(Dollar, 'Dollar'),
(Euro, 'Euro')
)
type = models.CharField(max_length=11, choices=TYPE_CHOICES, default=Dollar)
par = models.PositiveIntegerField()
year = IntegerRangeField()
size = models.CharField(max_length=7)
sign = models.TextField(max_length=250)
desc = models.TextField(max_length=500)
image = models.ImageField(upload_to='bons_images')
def __str__(self):
return str(self.par) + ' ' + self.type + ' ' + str(self.year.lower) + '-' + str(self.year.upper)
官方documentation说:
Regardless of the bounds specified when saving the data, PostgreSQL
always returns a range in a canonical form that includes the lower
bound and excludes the upper bound; that is [).
所以恐怕你无法真正改变它。但是,您可以像往常一样存储数据:
from psycopg2.extras import NumericRange
Bancnote.objects.create(
# specify other fields
year=NumericRange(1992, 1997, bounds='[]')
)
# assuming the object we created is with id=1
bancnote = Bancnote.objects.get(id=1)
print(bancnote.year) # NumericRange(1992, 1998, '[)')
我想你会的。帮助不大,但我能做的就这些了。
我有一个包含 IntegerRangeField 的模型,其中 returns 数据例如 NumericRange(1992, 1997, '[)') 将对象添加到数据库后。我需要范围的第二个选项也包含在其中,所以边界应该像 '[]'。 我在 psycopg2 documentation 中寻找解决方案,但不幸的是没有找到答案。
请帮忙!提前致谢。
我现在的models.py
from django.contrib.postgres.fields import IntegerRangeField
from django.contrib.postgres.validators import RangeMinValueValidator, RangeMaxValueValidator
from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models
from datetime import datetime
class Bancnote(models.Model):
Dollar= 'Dollar'
Euro= 'Euro'
TYPE_CHOICES = (
(Dollar, 'Dollar'),
(Euro, 'Euro')
)
type = models.CharField(max_length=11, choices=TYPE_CHOICES, default=Dollar)
par = models.PositiveIntegerField()
year = IntegerRangeField()
size = models.CharField(max_length=7)
sign = models.TextField(max_length=250)
desc = models.TextField(max_length=500)
image = models.ImageField(upload_to='bons_images')
def __str__(self):
return str(self.par) + ' ' + self.type + ' ' + str(self.year.lower) + '-' + str(self.year.upper)
官方documentation说:
Regardless of the bounds specified when saving the data, PostgreSQL always returns a range in a canonical form that includes the lower bound and excludes the upper bound; that is [).
所以恐怕你无法真正改变它。但是,您可以像往常一样存储数据:
from psycopg2.extras import NumericRange
Bancnote.objects.create(
# specify other fields
year=NumericRange(1992, 1997, bounds='[]')
)
# assuming the object we created is with id=1
bancnote = Bancnote.objects.get(id=1)
print(bancnote.year) # NumericRange(1992, 1998, '[)')
我想你会的。帮助不大,但我能做的就这些了。