Django DateField

53 views
Skip to first unread message

dtdave

unread,
Jul 5, 2018, 5:05:47 PM7/5/18
to Django users
I have a model that contains a datefield which signifies the start date for a task.
My modelmanager filters the tasks by the start date and the template lists them in the date descending order. Everything is fine with this.

However, now I have been asked to change this so that some projects have a start date of ASAP and others have date.
These then need to be listed in my template with ASAP tasks coming first and then those with a start date coming in descending order.

I am at a loss as to how to achieve this so would welcome any pointers or ideas.
Thanks

Jim Illback

unread,
Jul 5, 2018, 6:49:24 PM7/5/18
to Django users
Add another field, say asap = model.BooleanField(default=False), sort by asap, date in reverse. The field asap True values will be sorted first, then the asap False will be sorted by date. You may want to ensure asap = False only if date is not None.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fd2be4aa-408f-4221-9950-ba7a9af96e9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
Jul 5, 2018, 7:08:30 PM7/5/18
to django...@googlegroups.com
On donderdag 5 juli 2018 19:05:47 CEST 'dtdave' via Django users wrote:

> However, now I have been asked to change this so that some projects have a
> start date of ASAP and others have date.
> These then need to be listed in my template with ASAP tasks coming first
> and then those with a start date coming in descending order.
>
> I am at a loss as to how to achieve this so would welcome any pointers or
> ideas.


Asap field is a boolean. Date field needs to be able to be blank and null.
Then:

tasks = Task.objects.order_by('asap', '-start_date')

Done :)
--
Melvyn Sopacua

Bill Torcaso

unread,
Jul 6, 2018, 2:05:43 PM7/6/18
to Django users

I wonder if this would work: represent ASAP as a legitimate DateTime value that is, say, 100 years in the future.  Then a simple reverse sort will display all of the ASAP tasks before any others.

This is a hack, and nothing but a hack.  But you could implement it in five minutes.

dtdave

unread,
Jul 6, 2018, 2:46:08 PM7/6/18
to Django users
Many thanks for the help on this. I have implemented the following:
models.py
start_date = models.DateField(null=True, blank=True,)
asap = models.BooleanField(default=False)

I have amend my manager so the order is right on the template.
The template is where I am going wrong.
This what I have started with in my template;
{% if 'job.asap' == 'True' %}
<p>Start Date: {{job.asap}}</p>
{% else %}

<p>Start Date:{{job.start_date}}</p>
{% endif %}

The start dates are listed in the correct date order but any of those tasks with an ASAP start show None as the start date in the template.
I know the answer is probably obvious, but for smoe raeson I cannot fathom it out at the moment.
Again thanks for all the help

Melvyn Sopacua

unread,
Jul 6, 2018, 3:13:35 PM7/6/18
to django...@googlegroups.com
On vrijdag 6 juli 2018 16:46:08 CEST 'dtdave' via Django users wrote:
> Many thanks for the help on this. I have implemented the following:
> models.py
> start_date = models.DateField(null=True, blank=True,)
> asap = models.BooleanField(default=False)
>
> I have amend my manager so the order is right on the template.
> The template is where I am going wrong.
> This what I have started with in my template;
> {% if 'job.asap' == 'True' %}
> <p>Start Date: {{job.asap}}</p>

{% if job.asap %}
<p>Start Date: {% trans "as soon as possible" %}</p>
{% else %}
<p> Start Date: {{job.start_date}}</p>
{% endif %}

--
Melvyn Sopacua

Swati Pushp

unread,
Jul 6, 2018, 3:21:36 PM7/6/18
to django...@googlegroups.com
you want date auto genrate ??

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

dtdave

unread,
Jul 9, 2018, 6:41:14 AM7/9/18
to Django users
Many Thanks
Reply all
Reply to author
Forward
0 new messages