Answer the question
In order to leave comments, you need to log in
How to implement Datetime widget in Django 2?
I'm using Django 2.2
In the model field:USE_TZ = True
class Training(UuidBasedModel):
client = models.ForeignKey('common.User', on_delete=models.CASCADE, verbose_name=_('Client'))
canceled = models.BooleanField(default=False, verbose_name=_('Canceled'))
cancel_reason = models.CharField(default='', max_length=255, blank=True, verbose_name=_('Cancel reason'))
training_datetime = models.DateTimeField(default=timezone.localtime, verbose_name=_('Training date and time'))
class TrainingForm(forms.ModelForm):
def clean(self):
if self.cleaned_data['client'].trainings_list.filter(training_datetime__date=self.cleaned_data['training_datetime'].date()).exists():
raise forms.ValidationError({'client': self.SECOND_TRAINING_FOR_SINGLE_CLIENT_ERROR})
class Meta:
model = models.Training
fields = '__all__'
widgets = {
'training_datetime': forms.DateTimeInput(attrs={'type': 'datetime-local'}),
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question