﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34962	Support for overriding result of model field values	Anas Abou Allaban	nobody	"We have many models with timestamp fields (ex. `created_at`, `updated_at`, etc.) that store `DateTimeField`s.
We use Django as a REST API for web and mobile clients written using JavaScript where time is stored as milliseconds since the UNIX epoch (i.e. `Date.now()`).

To minimize the amount of processing we do client side, we convert `DateTimeField`s to milliseconds using a Django Rest Framework (DRF) serializer that looks like this:

{{{
class TimestampDateTimeField(serializers.DateTimeField):
    """"""Convert Python datetime to/from Unix timestamp in milliseconds.
    For use with JavaScript's Date objects which use ms since epoch, not seconds.
    """"""

    def to_representation(self, value):
        return round(value.timestamp() * 1000)

    def to_internal_value(self, value):
        try:
            result = datetime.fromtimestamp(value / 1000, timezone.get_default_timezone())
            return super(TimestampDateTimeField, self).to_internal_value(result)
        except TypeError:
            raise serializers.ValidationError(""Datetime must be a number, not a string"")
}}}

Ideally, we could instead do this data manipulation at the database layer using the `Extract` function **but** with the same behavior as the serializer where we can override the original name of the field.

Example:

{{{
# This will currently throw an error
# ValueError: The annotation 'updated_at' conflicts with a field on the model.
m = MyModel.objects.filter(user=user).annotate(
  updated_at=Extract(""updated_at"", ""epoch"") * 1000
)
}}}



The way we can do this instead is using the `extra()` method from the `QuerySet` API to generate a query that would look like:

{{{
SELECT (EXTRACT(EPOCH FROM app_mymodel.updated_at) * 1000) AS updated_at
FROM app_mymodel 
WHERE app_mymodel.user_id = 42
LIMIT 1;
}}}

I did my best to look online and in the docs for potential solutions/workarounds, but all point back to using the `extra()`. Is there a way to do this w/o using `extra()`?"	New feature	closed	Database layer (models, ORM)	4.2	Normal	wontfix	QuerySet.extra		Unreviewed	0	0	0	0	0	0
