Django: DateTimeField, how to make require only date. - django

I want to have DateTimeField(Not separate DateField and TimeField) but i want only date to be required or only time to have default value. Is it possible and if yes .. how :)

Related

Django query by separate date and time fields (how to perform conditional order_by)

I have a Meeting model. It has a separate date and time field, the reason why it is not a single datetime field is that the time is allowed to be null.
If I used a single datetime field and set the hours/minutes to 00:00 if no time is given, then I can't distinguish that from a meeting set at 00:00. And some unusual sentinel value like 13:37:42 as no meeting is a weird hack.
Here's the model:
class Meeting(models.Model):
meeting_date = models.DateField(db_index=True)
meeting_time = models.TimeField(db_index=True, null=True, blank=True)
Now the problem comes in to filter by a given date/time.
So I think the solution is to filter by date, then order by the time. E.g. to get the latest meeting that is before now:
from datetime import datetime
now = datetime.now()
prev_meeting = (
Meeting.objects.filter(meeting_date__lte=now.date())
.order_by('-meeting_date', f'meeting_time <= {now.time()}', '-meeting_time')
.first()
)
The f'meeting_time <= {now.time()}' is the part I don't know how to do with the django ORM, but I know how to do with SQL. the Django Docs do not mention conditional ordering.
The solution I came up with is to use a single datetime field to store the date/time, and then instead of a sentinel time value, I have an additional boolean value storing whether the time was set or not. If the time was not set, then then datetime time is 00:00 and the time_was_set field is set to False.

Order by time of a Datetime field in django

I wanted to order a query set on the basis of a time of a datetime field.
I have used the following (here Tasks is my model and datetime is the field)
Tasks.objects.all().order_by('datetime.time')
this doesn't work and also
Tasks.objects.all().order_by('datetime__time')
doesn't work as it is part of the same model.
I tried using .annotate() but I don't know how exactly to do it.
How should I go about doing this?
Tasks.objects.all().order_by('datetime__hour')
or
Tasks.objects.all().order_by('datetime__minute')
Task.objects.all().order_by('datetime__hour', 'datetime__minute')

Order by Datefield only in Django

I want to sort by DateTimeField ignoring one hour.
Because in my ordination I need
Person.objects.order_by('-date_joined', 'full_name')
Sorting by name has no effect because it is a Timefield , but I wanted to data.
Depending on your Django version (1.9+), this should work:
Person.objects.order_by ('-date_joined__date','full_name')
Otherwise you can use .extra to cast into date field:
Person.objects.extra(
select={'joined_date': 'DATE(myapp_person.date_joined)'},
order_by=['-joined_date', 'full_name'],
)

Django Model DatetimeField without seconds

I want to enter date and time in my admin site with
date = models.DateTimeField()
But I dont want to have the seconds in my view. Is it possible to display the time like this:
10:45 instead of 10:45:00 ?
Yes you can:
https://docs.djangoproject.com/en/dev/ref/forms/fields/#datetimefield
input_formats
A list of formats used to attempt to convert a string to a valid datetime.datetime object.
If no input_formats argument is provided, the default input formats are:
Thats if you want to save as well with no seconds, If you only want to change how its displayed in your templates your can use the date template filter:
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#date

Django DateTimeField with optional time part

I have a field which will represent the start time of an event and I am using the Django DateTimeField for this.
This field is mandatory but sometimes the users will only know the start date and not the time.
Is there anyway to make the time part optional and keep the date part mandatory?
Maybe you should try to separate date from time. There are DateField and TimeField for that.
Example for use at the views or models:
You can use function strptime to show the datetime field any formats.
from datetime import datetime
datetime.now().strftime('%Y-%m-%d')
# print string '2013-06-25'
Example for use at the templates:
you can use templatetag date
{{ datetime_field|date:"Y-m-d" }}