使用django表单在datetime字段中输入可选日期

时间:2014-08-31 12:06:10

标签: python django

class Pasthistory(models.Model):
    """
        To store Work Experience of the Employee.
        As of now no restriction on number of Past histories.
    """

    EMPLOYER_TYPES =(
        ("IND","Individual"),
        ("COR","Corporate"),
        )
    SLAB_CHOICES = (
        ('H', 'Hourly'),
        ('W', 'Weekly'),
        ('M', 'Monthly'),
    )
    # Choices for gender
    salary_slab         = models.CharField(max_length=1,choices=SLAB_CHOICES,default='M')
    employee_id         = models.ForeignKey(Employee,verbose_name="employee id")
    worked              = models.ForeignKey(Jobs,verbose_name="job",default=0,null=True,blank=True)
    experience          = models.IntegerField(verbose_name="Experience in years",null=True,blank=True,default=0)
    experience_mon      = models.IntegerField(verbose_name="Experience in months",null=True,blank=True,default=0)
    started_from        = models.DateField()
    worked_till         = models.DateField()
    employer_type       = models.CharField(max_length=3,choices=EMPLOYER_TYPES,default="COR")
    company_name        = models.CharField(max_length=50,null=True,blank=True)
    name_boss           = models.CharField(max_length=50,blank=True,null=True)
    phone               = models.CharField(max_length=11,null=True,blank=True ,validators=[RegexValidator(regex='^\d{10}$', message='Length has to be 10 digits for mobile number or 11 digits for landline numbers', code='Invalid number')])
    email               = models.EmailField(null=True,blank=True)
    work_description    = models.TextField(max_length=255,null=True,blank=True)
    salary_drawn        = models.CharField(max_length=30,null=True,blank=True)
    reason_for_leaving  = models.TextField(max_length=500,null=True,blank=True)


class exp_form(ModelForm):
    class Meta:
        model = Pasthistory
        exclude = ['employee_id']
        css_class   =  "form-control"
        select_class = 'form-control m-bot15'
        widgets = {
                    'worked'            :   forms.Select(       attrs   =   {'class'    :  select_class}),
                    'started_from'      :   forms.TextInput(    attrs   =   {'class'    :   css_class,'placeholder':'YYYY-MM-DD or MM/DD/YYYY'}),
                    'worked_till'       :   forms.TextInput(    attrs   =   {'class'    :   css_class,'placeholder':'YYYY-MM-DD or MM/DD/YYYY'}),
                    'salary_slab'       :   forms.Select(   attrs   =   {'class'    :   select_class}),
                    'employer_type'     :   forms.RadioSelect(),
                    'company_name'      :   forms.TextInput(    attrs   =   {'class'    :   css_class}),
                    'name_boss'         :   forms.TextInput(    attrs   =   {'class'    :   css_class}),
                    'experience'      :   forms.TextInput(    attrs   =   {'class'    :   css_class}),
                    'experience_mon'         :   forms.TextInput(    attrs   =   {'class'    :   css_class}),
                    'phone'             :   forms.TextInput(    attrs   =   {'class'    :   css_class,'placeholder':"Enter the phone number"}),
                    'email'             :   forms.TextInput(    attrs   =   {'class'    :   css_class,'placeholder':"Enter the email id"}),
                    'work_description'  :   forms.Textarea(     attrs   =   {'class'    :   css_class,'placeholder':'What all responsibilities did you perform at the job?'}),
                    'salary_drawn'      :   forms.TextInput(    attrs   =   {'class'    :   css_class}),
                    'reason_for_leaving':   forms.Textarea(     attrs   =   {'class'    :css_class,'placeholder':'Why did you leave / terminated from the job?'}),
        }

        def clean_worked_till(self):
            if self.cleaned_data['worked_till'] <= self.cleaned_data['started_from']:
                raise forms.ValidationError((u'Worked till has to be greater than Started from'))
            return self.cleaned_data['worked_till']

我想只从前端输入月份和年份并将其存储到模型中。如何通过django表格来完成?

在将输入存储到模型中之前,是否需要将一天添加到输入中?

0 个答案:

没有答案