gpt4 book ai didi

python - 如何避免 Django 模型中的日期重叠?

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:11 25 4
gpt4 key购买 nike

我有一个模型:

class Dimension_Item(models.Model):
uq_dimension_item_id = MyCharField(max_length=1024, primary_key=True)
dimension_id = MyCharField(max_length=1024)
dimension_item_id = MyCharField(max_length=100)
name = MyCharField(max_length=255)
description = MyCharField(max_length=512, null = True)
start_date = models.DateField(default=date(1,1,1))
end_date = models.DateField(default=date(9999,12,31))

向模型添加信息的函数:

def addRows(in_args):
rq = in_args[0]
pk_l=[]
rows = rq['rows']
if type(rows).__name__ == 'dict':
dim = Dimension_Item(
name=rows['name'],
start_date=rows['start_date'],
end_date=rows['end_date']
)
dim.save()
pk_l.append(dim.dimension_id)
elif type(rows).__name__ == 'list':
for i in rows:
dim = Dimension_Item(
name=rows['name'],
start_date=rows['start_date'],
end_date=rows['end_date']
)
dim.save()
pk_l.append(dim.dimension_id)
else:
pass
return getRows(in_args, pk_l)
# return "success add row"
clauses.addMethod(addRows)

以及修改模型项的函数:

def modifyRows(in_args):
pk_l=[]
rq = in_args[0]
rows = rq['rows']
if type(rows).__name__ == 'dict':
dim = Dimension_Item.objects.get(pk=rows['pk'])
for attr in rows:
if attr!='pk':
try:
setattr(dim, attr, rows[attr])
except KeyError:
pass
dim.save()
pk_l.append(dim.dimension_id)
elif type(rows).__name__ == 'list':
for i in rows:
dim = Dimension_Item.objects.get(pk=i['pk'])
for attr in i:
if i!='pk':
try:
setattr(dim, attr, i[attr])
except KeyError:
pass
dim.save()
pk_l.append(dim.dimension_id)
else:
pass
return getRows(in_args, pk_l)
# return "success modify"
clauses.addMethod(modifyRows)

我应该检查 start_date 和 end_date 字段是否不与数据库中的其他记录重叠。

例如,我输入:2.02.1988-3.07.1989。如果我已经有 2.07.1989 - 3.08.1990 的记录,我必须抛出有关日期重叠的异常。

我怎样才能做得更好?

最佳答案

我将重写 Dimension_Item 模型的 save() 方法。

在您的自定义 save() 方法中,您可以实现对重叠日期的检查。如果一切正常,则创建该对象。如果没有,则不返回任何内容(或引发错误。)

Django 文档解释得很好:https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

这里有一些(未经测试的)代码可以帮助您入门:

class Dimension_Item(models.Model):
start_date = models.DateField(default=date(1,1,1))
end_date = models.DateField(default=date(9999,12,31))

def save(self, *args, **kwargs):
# get number of items that have an overlapping start date
dimension_items_overlapping_start = Dimension_Item.objects.filter(start_date__gte=self.start_date, start_date__lte=self.end_date).count()

# get number of items that have an overlapping end date
dimension_items_overlapping_end = Dimension_Item.objects.filter(end_date__gte=self.start_date, end_date__lte=self.end_date).count()

overlapping_dimension_items_present = dimension_items_overlapping_start > 0 or dimension_items_overlapping_end > 0

if overlapping_dimension_items_present:
return
else:
super(Dimension_Item, self).save(*args, **kwargs) # Call the "real" save() method.

关于python - 如何避免 Django 模型中的日期重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29204825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com