6

I am using Python 3.6 and Django as web framework I have to save datetime together in my database but show date and time separate field in HTML. Model Code:

class date_time(models.Model):
    date = models.DateField(default=timezone.now)
    time = models.TimeField(default=timezone.now)

Form Code:

class date_time_Form(forms.ModelForm):
    class Meta:
        model = date_time
        fields = ('date', 'time',)
        widgets = {
            'date': forms.TextInput(attrs={'class': 'form-control', 'type': 'date'}),
            'time': forms.TextInput(attrs={'class': 'form-control', 'type': 'time'}),
        }
        labels = {
            'date': 'Date',
            'time': 'Time',
        }

Currently, I am storing in a separate field but I want to save in one field date+time.

Update - I can use Datetime field but that will create only one element in HTML page. When I use Datetime field

But in HTML I want separate fields like below with only one datetime field in model Desired output

Is there any way to achieve this?

displayname
  • 317
  • 1
  • 5
  • 15

5 Answers5

3

You can use SplitDateTimeField: https://docs.djangoproject.com/en/3.0/ref/forms/fields/#splitdatetimefield

class Myform(forms.Form):
    start_date = forms.SplitDateTimeField(initial=datetime.datetime.now)

Example of SplitDateTime field

Altough, if you want a label for each one, you will need to modify your template, or use separate form attributes.

Checo R
  • 862
  • 14
  • 22
0

That is exactly what DateTimeField is for.

date_time = models.DateTimeField()
Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91
  • I think I was not able to make you guys understand what I am trying to achieve. Please see the updated questions. Thank you for your efforts – displayname Sep 02 '17 at 00:52
0

You can use models.DateTimeField to store dateTime in one field. DateTimeField

Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
  • I think I was not able to make you guys understand what I am trying to achieve. Please see the updated questions. Thank you for your efforts – displayname Sep 02 '17 at 00:51
0

You can get the date and time from the HTML, then combine them into a DateTime object. See Pythonic way to add datetime.date and datetime.time objects for how.

If you want the reverse (taking a DateTime, and getting the date and time), you can see How do I convert datetime to date (in Python)? and Convert datetime to time in python

Eric
  • 730
  • 4
  • 16
  • Yes, but how we will implement this in Django. Is there a way to convert datetime field in model to date and time field in form? – displayname Sep 02 '17 at 01:17
  • 1
    @umang Django is Python. You store the DateTime value of the model in a variable, for example myDateTime, then call myDateTime.date() and myDateTime.time() to get date and time. – Eric Sep 02 '17 at 01:34
  • See, I have a column in database 'date_time' which saves date and time together. But In HTML template, I want Date and Time separate which means my date will come from one input field and time will come from another input field and then combine both and store it into our date_time column. So challenge here is if I create Datetime field in model.py then in html also I get one input field date and time together. And if I use datefield and timefield in separately in model.py then it will be stored as two separte column. I want one column in database and two separate input fields in HTML. – displayname Sep 02 '17 at 01:44
  • Then you use the first link I provided "add datetime.date and datetime.time objects together" to create a datetime.datetime object that you can store in the database. – Eric Sep 02 '17 at 02:05
0

The DateTimeField format is the following:

2022-04-23 00:22:55.654914+00:00

All you have to do, is load your DateTimeField variable in the view, and get the separate elements before passing them to context:

myobj = MyModel.objects.get(id=id)
dtf = myobj.datetimefield

date = str(dtf)[:10]
time = str(dtf)[11:19]

Now you can pass date and time in your context and display them.

When getting data back from HTML to Django, all you can do is put back the data together dtf = date + time and pass it to the object and save. I know this will only yield this format 2022-04-23 00:22:55 but it works, and you can also add the milliseconds and timezone if you want, or setup a default one.

Kaiss B.
  • 249
  • 1
  • 12