Converting string into datetime within Django modelform - django

I would like to accept natural language strings into a DateTime field, on a form generated by a Django ModelForm. I found Converting string into datetime, which explains how to convert the input string into a DateTime object. (In this case, I'm using timestring for the conversion, instead of strptime as suggested in the answers, because I want to handle input like 'tomorrow'.) But I can't figure out where code this like should be placed within the ModelForm code. If the conversion code is placed in form_valid, it never gets run, because is_python runs first and complains that the text input is not a DateTime. When I override is_python, I get an error that I think comes from some kind of recursive loop.
Relevant code:
models.py
class Widget(models.Model):
name = models.CharField(max_length=100)
widget_date = models.DateTimeField
forms.py
from timestring import Date
class NaturalDateField(forms.DateField):
def to_python(self, value):
if not value:
return none
return Date(value, tz=timezone.get_current_timezone())
class WidgetForm(forms.ModelForm):
widget_date = NaturalDateField()
class Meta:
model = Widget
fields = ['name', 'widget_date']
views.py
class WidgetUpdate(UpdateView):
model = Widget
form_class = WidgetForm
The error on submit is Invlid date string >>. Tracing the code shows that the initial input string converts correctly (to something like '2014-12-26 00:00:00-08:00'), but then the validate() function from site-packages/django/forms/fields.py runs and that goes back into the timestring package for some reason and tries to run def __eq__(self, other): from Date.py, which I think tries to run Date(other), which fails since other is blank.
What is the best method to accept a text string in a ModelForm and then convert it to a field-specific string such as DateTime to be saved in the database?

Looking at that project, your code will return a timestring.Date object, which Django doesn't know what to do with. You probably just need to get the date value from there:
def to_python(self, value):
if not value:
return none
parsed_date = Date(value, tz=timezone.get_current_timezone())
return parsed_date.date

Try reusing builtin django date parsing methods:
class NaturalDateField(forms.DateField):
def to_python(self, value):
value = super(NaturalDateField, self).to_python(value)
return value.replace(tzinfo=timezone.get_current_timezone())

Related

Remove Z from DateTimeField in serializer

Is there a way to make my serializer print the datetimefield by default like this
2022-03-28T00:00:00+00:00
Instead of this
2022-03-23T03:16:00Z
I get the first output when I do this
return obj.time.isoformat()
Cause
If you look into the code of django-rest-framework in serializers.DateTimeField if datetime is UTC timezone, the UTC offset (+00:00) will get converted to Z as can be seen here
Solution
If you want to make it reusable for DateTimeField, you need to create a custom serializer DateTimeField that inherits from serializers.DateTimeField and override the to_representation method by coping codes from django-rest-framework and removing lines that convert UTC offset string to Z.
from restframework import ISO_8601
from restframework import serializers
class CustomDateTimeField(serializers.DateTimeField):
def to_representation(self, value):
if not value:
return None
output_format = getattr(self, 'format', api_settings.DATETIME_FORMAT)
if output_format is None or isinstance(value, str):
return value
value = self.enforce_timezone(value)
if output_format.lower() == ISO_8601:
value = value.isoformat()
# remove lines that convert "+00:00" to "Z"
# See https://github.com/encode/django-rest-framework/blob/f4cf0260bf3c9323e798325702be690ca25949ca/rest_framework/fields.py#L1239:L1240
return value
return value.strftime(output_format)
Then use this in your serializer instead of serializers.DateTimeField
class MySerializer(serializers.Serializer):
datetime = CustomDateTimeField()
Extra
If you want to use it in serializers.ModelSerializer, you need to follow below steps
Create a custom ModelSerializer that inherits from serializers.ModelSerializer and set serializer_field_mapping attribute as follows
class CustomModelSerializer(serializers.ModelSerializer):
serializer_field_mapping = {
# copy serializer_field_mapping
**serializers.ModelSerializer.serializer_field_mapping,
# override models.DateTimeField to map to CustomDateTimeField
models.DateTimeField: CustomDateTimeField,
}
Use CustomModelSerializer instead of serializers.ModelSerializer. E.g.
class LogSerializer(CustomModelSerializer):
class Meta:
model = Log
fields = ["id", "created_at"]
The simplest way to do it is to specify the format you want in your serializer:
class MySerializer(serializer.Serializer):
my_date = serializers.DateTimeField(format='%Y-%m-%dT%H:%M') # Specify your format here

Change DateInput widget's format to SHORT_DATE_FORMAT

I'm trying to set the format of a DateInput to SHORT_DATE_FORMAT. However, the following code does not work work.
forms.py
from django.conf.global_settings import SHORT_DATE_FORMAT
class EventForm(ModelForm):
# ...
startDate = forms.DateField(
widget=forms.DateInput(
format=SHORT_DATE_FORMAT
)
)
In the views.py, an example entry is read from the database and a form is created using form = EventForm(instance=event1). The template then shows that widget using {{form.startDate}}. The input shows up correctly, only it's value is not a date but just "m/d/Y".
It would work if I'd set format='%m/%d/%Y', however, that defeats the purpose of the locale-aware SHORT_DATE_FORMAT. How can I properly solve that?
A possible solution is to overwrite the DateInput widget as follows.
from django.template.defaultfilters import date
class ShortFormatDateInput(DateInput):
def __init__(self, attrs=None):
super(DateInput, self).__init__(attrs)
def _format_value(self, value):
return date(value, formats.get_format("SHORT_DATE_FORMAT"))
It overwrites the super constructor so that the date format cannot be changed manually anymore. Instead I'd like to show the date as defined in SHORT_DATE_FORMAT. To do that, the _format_value method can be overwritten to reuse the date method defined django.templates.

Filtering on DateTimeField with Django Rest Framework

I have a model with a DateTimeField:
class MyShell(models):
created = models.DateTimeField(auto_now=true)
I have an api linked to it using Django Rest Framework:
class ShellMessageFilter(django_filters.FilterSet):
created = django_filters.DateTimeFilter(name="created",lookup_type="gte")
class Meta:
model = ShellMessage
fields = ['created']
class ShellListViewSet(viewsets.ModelViewSet):
"""
List all ShellMessages
"""
serializer_class = ShellMessageSerializer
queryset = ShellMessage.objects.all()
filter_class = ShellMessageFilter
When I hit my API using the following URL it works perfectly:
http://127.0.0.1:8000/api/shell/?created=2014-07-17
# It returns all shell with a date greater than the one provided in URL
But, I want to do more than that by filtering base on a date and a time. I tried the following URL without success:
http://127.0.0.1:8000/api/shell/?created=2014-07-17T10:36:34.960Z
# It returns an empty array whereas there are items with a created field greater than 2014-07-17T10:36:34.960Z
If you guys know how to proceed... I don't find any good informations or example in django-filters documentation...
Simpler solution if you don't care about fractions of seconds: replace the "T" with space (%20):
http://127.0.0.1:8000/api/shell/?created=2014-07-17%2010:36:34
Worked for me.
This may not be what you want, but you could simply convert from Unix time. E.g.:
def filter_unix_dt(queryset, value):
if not value:
return queryset
try:
unix_time = int(value)
t = datetime.fromtimestamp(unix_time)
result = queryset.filter(created__gte=t)
return result
except ValueError:
return queryset
class ShellMessageFilter(django_filters.FilterSet):
created = django_filters.DateTimeFilter(action=filter_unix_dt)
class Meta:
model = ShellMessage
fields = ['created']
The issue and solution are documented in this DRF issue page: https://github.com/tomchristie/django-rest-framework/issues/1338
TL;DR: A Django ISO conversion 'issue' is preventing DRF from working as you are expecting. A fix for this has been written in DRF, allowing you to use IsoDateTimeField instead of DateTimeField. Just replaying the T with a space in your request param value also works.

How to I change the rendering of a specific field type in Django admin?

For example I have an IntegerField and I want to change how it is displayed all across Django admin.
I considered subclassing it and overriding __str__ and __unicode__ methods but it doesn't seam to work.
class Duration(models.IntegerField):
def __unicode__(self):
return "x" + str(datetime.timedelta(0, self))
def __str__(self):
return "y" + str(datetime.timedelta(0, self))
Update: I just want to chage the way the field is displayed, not the edit control (widget).
I'm not sure what you want to do with the field, but if you want to change the HTML that is displayed, you need to either change the widget that the form field is using, or create your own custom widget:
https://docs.djangoproject.com/en/dev/ref/forms/widgets/
models.py
class LovelyModel(models.Model):
my_int = models.IntegerField()
forms.py
from widgets import WhateverWidgetIWant
class LovelyModelForm(forms.ModelForm):
my_int = models.IntegerField(widget=WhateverWidgetIWant())
class Meta:
model = LovelyModel
admin.py
from forms import LovelyModelForm
class LovelyModelAdmin(admin.ModelAdmin):
form = LovelyModelForm
What is it you are trying to do?
I think you need something like this (untested code)::
import datetime
from django.db import models
class Duration(models.IntegerField):
description = "Stores the number of seconds as integer, displays as time"
def to_python(self, value):
# this method can receive the value right out of the db, or an instance
if isinstance(value, models.IntegerField):
# if an instance, return the instance
return value
else:
# otherwise, return our fancy time representation
# assuming we have a number of seconds in the db
return "x" + str(datetime.timedelta(0, value))
def get_db_prep_value(self, value):
# this method catches value right before sending to db
# split the string, skipping first character
hours, minutes, seconds = map(int, value[1:].split(':'))
delta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
return delta.seconds
This, however, changes how the field's value represented in Python at all, not only in admin, which may not be a desired behaviour. I.e., you have object.duration == 'x00:1:12', which would be saved to the database as 72.
See also documentation on custom fields.

Django model fields getter / setter

is there something like getters and setters for django model's fields?
For example, I have a text field in which i need to make a string replace before it get saved (in the admin panel, for both insert and update operations) and make another, different replace each time it is read. Those string replace are dynamic and need to be done at the moment of saving and reading.
As I'm using python 2.5, I cannot use python 2.6 getters / setters.
Any help?
You can also override setattr and getattr. For example, say you wanted to mark a field dirty, you might have something like this:
class MyModel:
_name_dirty = False
name = models.TextField()
def __setattr__(self, attrname, val):
super(MyModel, self).__setattr__(attrname, val)
self._name_dirty = (attrname == 'name')
def __getattr__(self, attrname):
if attrname == 'name' and self._name_dirty:
raise('You should get a clean copy or save this object.')
return super(MyModel, self).__getattr__(attrname)
You can add a pre_save signal handler to the Model you want to save which updates the values before they get saved to the database.
It's not quite the same as a setter function since the values will remain in their incorrect format until the value is saved. If that's an acceptable compromise for your situation then signals are the easiest way to achieve this without working around Django's ORM.
Edit:
In your situation standard Python properties are probably the way to go with this. There's a long standing ticket to add proper getter/setter support to Django but it's not a simple issue to resolve.
You can add the property fields to the admin using the techniques in this blog post
Overriding setattr is a good solution except that this can cause problems initializing the ORM object from the DB. However, there is a trick to get around this, and it's universal.
class MyModel(models.Model):
foo = models.CharField(max_length = 20)
bar = models.CharField(max_length = 20)
def __setattr__(self, attrname, val):
setter_func = 'setter_' + attrname
if attrname in self.__dict__ and callable(getattr(self, setter_func, None)):
super(MyModel, self).__setattr__(attrname, getattr(self, setter_func)(val))
else:
super(MyModel, self).__setattr__(attrname, val)
def setter_foo(self, val):
return val.upper()
The secret is 'attrname in self.__dict__'. When the model initializes either from new or hydrated from the __dict__!
While I was researching the problem, I came across the solution with property decorator.
For example, if you have
class MyClass(models.Model):
my_date = models.DateField()
you can turn it into
class MyClass(models.Model):
_my_date = models.DateField(
db_column="my_date", # allows to avoid migrating to a different column
)
#property
def my_date(self):
return self._my_date
#my_date.setter
def my_date(self, value):
if value > datetime.date.today():
logger.warning("The date chosen was in the future.")
self._my_date = value
and avoid any migrations.
Source: https://www.stavros.io/posts/how-replace-django-model-field-property/