任何机构都可以建议我如何比较django-admin中的两个日期字段 我是管理员,我想说每当我更新日期字段的日期时,其他日期字段也必须更改(加上第一天的30天),如果第二个日期字段已更改,我想更改另一个字段只是在django管理页面! 提前谢谢
STATUS_CHOICES = (
('P', 'Paid'),
('U', 'UNPAID'),
)
class Users(models.Model):
status = models.CharField(max_length=1, choices=STATUS_CHOICES , default="U")
updated = models.DateTimeField(default=now)
expiry = models.DateTimeField(default=updated_plus_30_days())
# updated_plus_30_days():
# if updated = "12/8/2017" and expiry is equal to "12/09/2017" and then i changed
# the updated field to be equal to "12/12/2017" then expiry have to equal to "12/1/2018"
# if the updated field is >= than expiray then expiray have to increase one month and check
# if status is == p:
# status = u
# else :
# status = u
答案 0 :(得分:1)
你可以试试这样的事情。
from datetime import datetime
#pip install python-dateutil
from dateutil.relativedelta import relativedelta
class Users(models.Model):
status = models.CharField(max_length=1, choices=STATUS_CHOICES , default="U")
updated = models.DateTimeField(default=now)
expiry = models.DateTimeField()
def save(self, *args, **kwargs):
self.expiry = self.updated + relativedelta(days=30)
super(Users, self).save(*args, **kwargs)
答案 1 :(得分:0)
你好,你可以查看这个
STATUS_CHOICES = (
('P', 'Paid'),
('U', 'UNPAID'),
)
from datetime import datetime, timedelta
class Users(models.Model):
status = models.CharField(max_length=1, choices=STATUS_CHOICES , default="U")
updated = models.DateTimeField(default=now)
expiry = models.DateTimeField()
def save(self):
self.expiry = default=self.updated+timedelta(days=30)
super(Users, self).save()