convert date from string to datetime

i want to convert date stored on a server in string using expressions
here the view:

class SubscribersListAPIView(ListAPIView):
    serializer_class = AbonesSerializer

    def get_queryset(self):
        start_date_param = self.request.query_params.get("start_date", None)
        end_date_param = self.request.query_params.get("end_date", None)

        start_date = None
        end_date = None

        if start_date_param:
                start_date = datetime.strptime(start_date_param, "%d/%m/%Y").date()
            except ValueError:

        if end_date_param:
                end_date = datetime.strptime(end_date_param, "%d/%m/%Y").date()
            except ValueError:

        queryset = Abones.objects.all()

        queryset = Abones.objects.annotate(
            date_as_datetime=ExpressionWrapper(F('date'), output_field=DateTimeField()

        if start_date_param and end_date_param:
            queryset = queryset.filter(date_as_datetime__range=(start_date, end_date))
        elif start_date_param:
            queryset = queryset.filter(date_as_datetime=start_date)
        elif end_date_param:
            queryset = queryset.filter(date_as_datetime=end_date)

        return queryset

but when i run the code they show me :
AttributeError: ‘DateTimeField’ object has no attribute ‘model’
where’s the problem??

Please post the full error message with the traceback, along with the model involved here.

Also, when you’re posting code or error messages, enclose the code (or error message) between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. (I’ve taken the liberty of editing your original post for this. Please remember to do this in future posts.)

Here the model

class Abones(models.Model):
    idu = models.IntegerField(db_column='idU')  # Field name made lowercase.
    nom = models.TextField()
    prenom = models.TextField()
    nom_pere = models.TextField()
    nom_mere = models.TextField()
    sexe = models.TextField()
    nationalite = models.TextField()
    date_naissance = models.TextField()
    identite = models.CharField(max_length=250)
    province_n = models.TextField()
    commune_n = models.TextField()
    zone_n = models.TextField()
    colline_n = models.TextField()
    residence = models.TextField()
    occupation = models.TextField(blank=True, null=True)
    recto = models.TextField()
    verso = models.TextField()
    photo = models.TextField()
    date = models.TextField()

    class Meta:
        managed = False
        db_table = 'abones'

This is the traceback:

Internal Server Error: /api/v1/subscribers-list
Traceback (most recent call last):
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/core/handlers/", line 55, in inner
response = get_response(request)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/core/handlers/", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/views/decorators/", line 56, in wrapper_view
return view_func(*args, **kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/views/generic/", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 509, in dispatch
response = self.handle_exception(exc)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 469, in handle_exception
 File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 480, in raise_uncaught_exception
raise exc
 File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 199, in get
return self.list(request, *args, **kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/rest_framework/", line 38, in list
queryset = self.filter_queryset(self.get_queryset())
File "/var/www/test/onasoft_test/onasoftadapter/apps/base/", line 106, in get_queryset
queryset = queryset.filter(date_as_datetime=start_date)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 1436, in filter
return self._filter_or_exclude(False, args, kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 1454, in _filter_or_exclude
 clone._filter_or_exclude_inplace(negate, args, kwargs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 1461, in _filter_or_exclude_inplace
 self._query.add_q(Q(*args, **kwargs))
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/sql/", line 1534, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/sql/", line 1565, in _add_q
child_clause, needed_inner = self.build_filter(
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/sql/", line 1433, in build_filter
condition = self.build_lookup(lookups, reffed_expression, value)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/sql/", line 1307, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 341, in get_prep_lookup
return super().get_prep_lookup()
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/fields/", line 1585, in get_prep_value
value = super().get_prep_value(value)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/fields/", line 1464, in get_prep_value
return self.to_python(value)
File "/var/www/envs/onasoft/lib/python3.10/site-packages/django/db/models/fields/", line 1538, in to_python
% (self.model.__name__,, value),
 AttributeError: 'DateTimeField' object has no attribute 'model'

In my opinion, it seems like the date field of the model is declared as a TextField.

What is the data type of the original column in the database?

If it is a Varchar (or similar untyped text field), what are a couple of the values you’re seeing in it?

If it’s only a date, then the proper field type would be a DateField and not a DateTimeField.

it is a TextField
There are some values that are like this


others like


You may need to Cast that field as a date rather than just using the output_field parameter.

Well, that’s closer - at least it’s telling you that there’s something in one of the rows that “doesn’t work”. You might want to examine the data more closely to see if there is any bad data in that field.

I tested more solutions, it didn’t work

I would write a query or a script that would check that column in the database to see if there are rows where the value in that column is not of the required format. It only takes one row to be improper for the error to be thrown.