简体   繁体   English

如何设计一个列以在Django模型中手动添加有效时间段

[英]how to design a column to add manually valid time period in django model

i have a model namely SubscribePlan here it is 我在这里有一个模型,即SubscribePlan

class SubscribePlan(models.Model):

    plan = models.CharField(max_length = 200)
    price = models.IntegerField(default=0)
    validity_period = models.DateTimeField()
    number_of_download = models.IntegerField(default = 0)

here i want to add a validity_period column in such a way where the admin will manually add the validity time.I know that django model provides two different datatypes for Date one is DateField() and another is DateTimeField() 在这里我想添加一个validity_period列, admin可以手动添加有效时间。我知道Django模型为Date提供了两种不同的数据类型,一种是DateField() ,另一种是DateTimeField()

and if i am not wrong ,there is two parameter for DateTimeField() ,one is auto_now and another is auto_now_add . 如果我没有记错的话, DateTimeField()有两个参数,一个是auto_now ,另一个是auto_now_add

UPDATE : 更新

But my concern is,i have to provide such a model for the admin where admin can manually put a valid time period assume 24 hours or 48 hours or 1 day or 2 days through the Admin interface or Admin panel. 但我担心的是,我必须为管理员提供这样的模型,其中管理员可以通过Admin界面或Admin面板手动设置一个有效时间段,假设24小时或48小时或1天或2天。

So in this case what should be the data type of the model where valid time period can be input manually? 那么在这种情况下,可以手动输入有效时间段的模型的data type应该是什么? or if the datatype is DateTimeField then what should be the parameter? 或者,如果datatypeDateTimeField那么参数应该是什么? in mention i am using django 1.5 提到我正在使用Django 1.5

I think the validity_period should be Integer Field(or Character Field), because it is not a Date but just a number which can be used for checking a time period, like this: 我认为validity_period应该是整数字段(或字符字段),因为它不是Date而只是一个可用于检查时间段的数字,如下所示:

For example, lets make a function which will check the validity of a Subscription. 例如,让我们做一个检查订阅有效性的函数。

for Integer Field 用于整数字段

import datetime
def check_validity(start_time ,validity_period):
    if datetime.datetime.now() <= start_time + datetime.timedelta(hours=validy_period):
      return True
    return False

It can also depend on what kind of control(web element) is being used in admin interface for entering the validity period. 它还可能取决于在管理界面中使用哪种控件(Web元素)来输入有效期。 If the admin interface has a date-time picker or calendar, which returns a DateTime object, for entering the validity period then in your model the validity_period field can be of type DateTimeField() or DateField(). 如果管理界面具有日期时间选择器或日历(返回一个DateTime对象)以输入有效期,则在模型中,validation_period字段的类型可以为DateTimeField()或DateField()。 But if admin is providing input like 1 days or 2 days then it should be of IntegerField or CharField. 但是,如果admin提供1天或2天之类的输入,则它应该是IntegerField或CharField。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM