Django models.DateTimeField,如何存储没有秒的值?
Django models.DateTimeField, how to store the value without seconds?
我有这个型号
class Appointments(models.Model):
options = (
('waiting', 'In Attesa'),
('confirmed', 'Confermato'),
('closed', 'Chiuso'),
)
duration = (
('15', '15 minuti'),
('20', '20 minuti'),
('30', '30 minuti'),
('40', '40 minuti'),
)
date = models.DateTimeField(default=timezone.now)
patient_first_name = models.CharField(max_length=250)
patient_last_name = models.CharField(max_length=250)
patient_email = models.EmailField(_('email address'))
patient_phone = models.CharField(max_length=250)
doctor = models.ForeignKey(Doctor, on_delete=models.PROTECT)
room = models.ForeignKey(Room, on_delete=models.PROTECT, default=1)
status = models.CharField(max_length=10, choices=options, default='waiting')
message = models.TextField(null=True, blank=True)
notes = models.TextField(null=True, blank=True)
appointment_date = models.DateTimeField(null=True, blank=True)
duration = models.CharField(max_length=10, choices=duration, default='15')
class Meta:
ordering = ('-date', )
unique_together = ('appointment_date', 'room', )
如何在数据库中存储没有秒数的 appointment_date
值?
现在的值是这样的 2021-11-05 17:30:43
我想将其存储为 2021-11-05 17:30
那是因为 unique_together
对我需要的东西基本上没用。
我写了 an article 关于构建 DateTimeField
的变体,它会截断为一周、一个月、一分钟等
在这种情况下,我们可以制作一个 MinuteDateTimeField
:
# <em>app_name</em>/fields.py
from datetime import timedelta
from django.db.models import DateTimeField
class <strong>DateTruncMixin</strong>:
def truncate_date(self, dt):
return dt
def to_python(self, value):
value = super().to_python(value)
if value is not None:
return self.truncate_date(value)
return value
class <strong>MinuteDateTimeField</strong>(<strong>DateTruncMixin</strong>, DateTimeField):
def truncate_date(self, dt):
return dt<strong>.replace(second=0, microsecond=0)</strong>
然后您可以将此 MinuteDateTimeField
用于:
# <em>app_name</em>/models.py
from app_name.fields import <strong>MinuteDateTimeField</strong>
class Appointments(models.Model):
# ⋮
appointment_date = <strong>MinuteDateTimeField(null=True, blank=True)</strong>
我有这个型号
class Appointments(models.Model):
options = (
('waiting', 'In Attesa'),
('confirmed', 'Confermato'),
('closed', 'Chiuso'),
)
duration = (
('15', '15 minuti'),
('20', '20 minuti'),
('30', '30 minuti'),
('40', '40 minuti'),
)
date = models.DateTimeField(default=timezone.now)
patient_first_name = models.CharField(max_length=250)
patient_last_name = models.CharField(max_length=250)
patient_email = models.EmailField(_('email address'))
patient_phone = models.CharField(max_length=250)
doctor = models.ForeignKey(Doctor, on_delete=models.PROTECT)
room = models.ForeignKey(Room, on_delete=models.PROTECT, default=1)
status = models.CharField(max_length=10, choices=options, default='waiting')
message = models.TextField(null=True, blank=True)
notes = models.TextField(null=True, blank=True)
appointment_date = models.DateTimeField(null=True, blank=True)
duration = models.CharField(max_length=10, choices=duration, default='15')
class Meta:
ordering = ('-date', )
unique_together = ('appointment_date', 'room', )
如何在数据库中存储没有秒数的 appointment_date
值?
现在的值是这样的 2021-11-05 17:30:43
我想将其存储为 2021-11-05 17:30
那是因为 unique_together
对我需要的东西基本上没用。
我写了 an article 关于构建 DateTimeField
的变体,它会截断为一周、一个月、一分钟等
在这种情况下,我们可以制作一个 MinuteDateTimeField
:
# <em>app_name</em>/fields.py
from datetime import timedelta
from django.db.models import DateTimeField
class <strong>DateTruncMixin</strong>:
def truncate_date(self, dt):
return dt
def to_python(self, value):
value = super().to_python(value)
if value is not None:
return self.truncate_date(value)
return value
class <strong>MinuteDateTimeField</strong>(<strong>DateTruncMixin</strong>, DateTimeField):
def truncate_date(self, dt):
return dt<strong>.replace(second=0, microsecond=0)</strong>
然后您可以将此 MinuteDateTimeField
用于:
# <em>app_name</em>/models.py
from app_name.fields import <strong>MinuteDateTimeField</strong>
class Appointments(models.Model):
# ⋮
appointment_date = <strong>MinuteDateTimeField(null=True, blank=True)</strong>