Django change the way date field is displayed in django admin - django

That's it i want to change the way its displayed cause it says "noon" or "midnight" and i want i to display the exact hour
I've got this code on the field
date = models.DateTimeField(auto_now_add=True, default=datetime.date.today())

You have 3 options.
Override DATETIME_FORMAT, read here: datetime format
Use a form for your model and in the date attribute have some like this(
in input format, put whatrever you want):
date = forms.DateTimeField(input_format="%b %d %Y %I:%M%p")
Or just change the way you render your datetime field in the template:
{{ your_model.date_field|date:'Y-m-d H:i' }}

Related

Django Changing Date Input Format

I have a simple booking form which is having a date input in a line like:
reservation.html
<textarea name="booking" class="form-control" id="reservation"
value="{{placement.date|date:'d/m/Y'}}"></textarea>
models.py
class Lib(models.Model):
booking = models.DateField(null=True, blank=True)
My database postgresql based and also store date as in "date" format. I would love to change date-input like dd/mm/yyyy
I also tried to use placement.date|date type format in double quotes like:
<textarea name="booking" class="form-control" id="reservation"
value="{{placement.date|date:"d/m/Y"}}"></textarea>
But its still accept as mm/dd/YY. Is there any problem about my placement? My forms.py kinda simple like:
from django.forms import ModelForm
from .models import Lib
from django.utils import formats
class LibForm(ModelForm):
class Meta:
model = Lib
fields = ['title','name','email','booking']
I searched on google and on stackoverflow and found something on Django - Setting date as date input value and django how to format date to DD/MM/YYYY but i couldn't find anything that could help me. I have to seek help here as a last resort. Thanks in advance.

auto_add = True updates with DateTimeField model field, but not DateField

I'm using a formset to update a single field of a large group of model instances. I'd like to display a time stamp showing the time since the field for that instance was last updated. Since this field will usually be updated once a week, I'd rather use a DateField than a DateTimeField for the time stamp. DateField doesn't seem to get updated on save though. When I change the model field to DateTimeField, however, it works as expected. Here's my code.
#Template
<div class = 'last-updated'> {{ form.instance.last_updated|timesince }} </div>
# Models.py
last_updated = models.DateField(auto_now=True)
# Models.py - This version works
last_updated = models.DateTimeField(auto_now=True)
I've found posts saying to override the model's save() method but this seems like a last resort, and the posts I've found saying this are dated from 2011 and earlier, so probably out of date.
Since the docs list DateField and DateTimeField as more or less the same, with the same optional arguments, I'm wondering why they don't seem to update auto_now in the same way.
Edit
It also looks as though when I change the field type to DateField, the value displayed is the time since creation, not the time since update, and it updates the value for every single item in the formset. To clarify, there is NO custom save method for this model.

Django - Date and Time Field Explanation

I have few questions regarding the Date and time fields both in the model and in the form class.
1.)In the docs for the date field what does the line "Normalizes to: A Python datetime.date object." mean? Does it mean that the field data stored in the request.POST dictionary on the submission of the form is a datetime.date object?If yes, then when does it do it when the form is submitted or do we have to call one of its functions?
2.)If i have a models.DateField() then while assigning data to it manually in a view should i assign a datetime.date object or a unicode object with the 'yyyy-mm-dd' format?
3.)If i take a date in my forms.DateField() in the '%d/%m/%y' format how do i assign it to my models.DateField() because that seems to accept only the 'YYYY-mm-dd' format?
If somebody could also suggest some links which explain these fields in detail with examples apart from the docs it would be helpful.
For first question, datefield saves date object and if you are saving any data( for example a string: "01-01-2015") to datefield, then you have to convert that data into date object. You will not get a date object in request.POST, if you use a form, then you can get it by using cleaned_data.(example below) From request.POST ,you will get an unicode object.( then you will need to convert it to date object, example below)
For second question, obviously you have to save dateobject, not unicode object.
For third question, in forms, To display initial field value properly formatted, use DateInput widget. To customize validation, use input_formats keyword argument of DateField. Here, from forms, you will get a date object in views, so it can be saved directly, like:
In model.py:
class DateModelClass(models.Model):
date= models.DateField()
In forms.py:
date= forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=('%d/%m/%Y',))
In views.py:
date_data= DateModelClass(date= form.cleaned_data['date'])
date_data.save()
Also you can convert a string of your desired format to Date Object like this:
>>import datetime
>>datetime.datetime.strptime(u"09/07/2014", '%d/%m/%Y').date()

Django DateTimeField

I have a field in my model
published_on = models.DateTimeField()
on a Django template page and I want to show only the date instead of date along with time. Any idea how to truncate the time form date in the django model form?
Thanks in advance.
Use the date filter in your template, for instance:
{{ form.published_on|date:"D d M Y" }}
Or just:
{{ form.published_on|date }}
You can customize the output the way you want, or use the locale default. See this link for details.
You could try to use SplitDateTimeWidget to represent the field in two widgets (https://docs.djangoproject.com/en/dev/ref/forms/widgets/#splitdatetimewidget). Then, for example, the time field could be hidden with CSS. This method is particularly useful if you need to preserve the actual time value, and just allow user to change the date.

How to set default date in SelectDateWidget in django

I am using SelectDateWidget widget for entering date in the form field. But I want it to show the current date by default. How can I do that?
model.py
bdate = models.DateField(default=datetime.date.today())
This is giving error. can anyone tell the correct way to do that?
Also, my in template
{{ form.bdate }}
when I use the above mentioned line in my template it displays like this -- -- --
but I want something like this month date year. how can i do this?
My form is:
widgets = {
'bdate' : SelectDateWidget(),
}
Using Django 1.3 the following worked for me:
join_date = forms.DateField(widget=SelectDateWidget(), label='Joining Date', initial=timezone.now())
The keyword is initial not default.
There was similar problem here
Try to use default=datetime.date.today or auto_now_add=True