ожидаемая строка или байтовоподобный объект Django PostgreSQL ошибка - PullRequest
0 голосов
/ 30 марта 2020

Привет, ребята, у меня возникает эта ошибка, когда я пытаюсь создать или обновить модель пользователя. Ошибка возникает в модели профиля.

Я использую Django 2.1.7 с PostgreSQL

Внутренняя ошибка сервера: / account / update /

Файл "E: \ PRACTI ~ 1 \ FULLST ~ 1 \ chatapp \ env \ lib \ site-packages \ django \ utils \ dateparse.py ", строка 106, в parse_datetime match = datetime_re.match (value) TypeError: ожидаемая строка или байтовоподобный объект

Ошибка трассировки

Internal Server Error: /accounts/update/
Traceback (most recent call last):
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\rest_framework\views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
    raise exc
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\rest_framework\views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "E:\practice my skills\fullStackDjangoAndReact\chatapp\accounts\api.py", line 73, in post
    user = user_ser.save()
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\rest_framework\serializers.py", line 207, in save
    self.instance = self.update(self.instance, validated_data)
  File "E:\practice my skills\fullStackDjangoAndReact\chatapp\accounts\serializers.py", line 67, in update
    profile.save()
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\base.py", line 812, in _save_table
    forced_update)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\base.py", line 861, in _do_update
    return filtered._update(values) > 0
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\query.py", line 712, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\sql\compiler.py", line 1383, in execute_sql
    cursor = super().execute_sql(result_type)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\sql\compiler.py", line 1052, in execute_sql
    sql, params = self.as_sql()
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\sql\compiler.py", line 1349, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\fields\__init__.py", line 1429, in get_db_prep_value
    value = self.get_prep_value(value)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\fields\__init__.py", line 1408, in get_prep_value
    value = super().get_prep_value(value)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\fields\__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\db\models\fields\__init__.py", line 1369, in to_python
    parsed = parse_datetime(value)
  File "E:\PRACTI~1\FULLST~1\chatapp\env\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

Модель профиля:

class Profile(models.Model):
    user = models.OneToOneField(User ,on_delete=models.CASCADE)
    icon = models.CharField(max_length=256)
    active = models.BooleanField(default=False)
    join_date = models.DateTimeField(default=timezone.now, blank=True, null=True)
    born_date = models.DateTimeField(blank=True, null=True)

Сериализатор

class UpdateUserSer(serializers.ModelSerializer):
    profile = UpdateProfileSer()

    class Meta:
        model = User
        fields = ("username","email","first_name","profile")

    def update(self , instance , validated_data):
        prfile_data = validated_data.pop("profile")
        instance.username = validated_data.get("username" , instance.username)
        instance.email = validated_data.get("email" , instance.email)
        instance.first_name = validated_data.get("first_name" , instance.first_name)
        instance.save()

        profile = instance.profile
        profile.icon = prfile_data.get("icon" ,profile.icon)
        profile.save()

        return instance

    def validate(self , data):
        user_instance = self.context["request"].user
        user_email = data["email"]
        users = User.objects.all()
        if users.filter(email = user_email).exclude(id=user_instance.id).exists():
            raise serializers.ValidationError("this email has been token choose another one please")
        return data

UpdateProfileSer

class UpdateProfileSer(serializers.ModelSerializer):
    class Meta:
        model = Profile 
        fields = ("icon",)
...