I have this view (Django Rest Framework) that is supposed to create a profile
class CreateProfileView(APIView):
permission_classes = [permissions.IsAuthenticated]
def post(self, request):
data = dict(request.data)
location = {}
location.update(street=data.pop('street'))
location.update(additional=data.pop('additional'))
location.update(country=data.pop('country'))
location.update(state=data.pop('state'))
location.update(city=data.pop('city'))
location.update(zip=data.pop('zip'))
location.update(phone=data.pop('phone'))
user_id = data.pop('user')
id = int((user_id[0]))
image = data.pop('photo')
user = User.objects.get(pk=id)
Profile.objects.create(**data)
return Response("Profile saved successfully")
When I try and create a profile I get an error:
TypeError: fromisoformat: argument must be str
Here is the traceback:
Traceback (most recent call last):
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/500GB/calvin/projects/postproj/website/backend/api/views/profiles.py", line 54, in post
Profile.objects.create(**data)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/query.py", line 671, in create
obj.save(force_insert=True, using=self.db)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/base.py", line 812, in save
self.save_base(
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/base.py", line 863, in save_base
updated = self._save_table(
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/base.py", line 1006, in _save_table
results = self._do_insert(
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/base.py", line 1047, in _do_insert
return manager._insert(
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/query.py", line 1790, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1659, in execute_sql
for sql, params in self.as_sql():
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in as_sql
value_rows = [
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1584, in <listcomp>
[
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1585, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1523, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 925, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1438, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1433, in get_prep_value
return self.to_python(value)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1389, in to_python
parsed = parse_date(value)
File "/home/calvin/.local/share/virtualenvs/website-OCaWupbT/lib/python3.8/site-packages/django/utils/dateparse.py", line 74, in parse_date
return datetime.date.fromisoformat(value)
TypeError: fromisoformat: argument must be str
Here is the profile model:
class Profile(models.Model):
# Gender
M = 'M'
F = 'F'
O = 'O'
GENDER = [
(M, "male"),
(F, "female"),
(O, "Other")
]
# Basic information
background = models.FileField(upload_to=background_to, null=True, blank=True)
photo = models.FileField(upload_to=image_to, null=True, blank=True)
slug = AutoSlugField(populate_from=['first_name', 'last_name', 'gender'])
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, null=True, blank=True)
last_name = models.CharField(max_length=100)
birthdate = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER, default=None)
bio = models.TextField(max_length=5000, null=True, blank=True)
languages = ArrayField(models.CharField(max_length=30, null=True, blank=True))
# Location information
website = models.URLField(max_length=256, null=True, blank=True)
# author information
user = models.OneToOneField(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")
class Meta:
verbose_name = "profile"
verbose_name_plural = "profiles"
db_table = "user_profiles"
def __str__(self):
return self.first_name + ' ' + self.last_name
def get_absolute_url(self):
return self.slug
If I print the data object I get the following:
{'first_name': ['Calvin'], 'middle_name': ['undefined'], 'last_name': ['Cani'], 'birthdate': ['1971-09-01'], 'gender': ['M'], 'bio': ['This is general information about me'], 'languages': [''], 'website': ['']}
I can create a profile in the shell without any problems and I enter the birthdate exactly the same.
Why the error please and how do I fix it?