N
N
NikClik2018-12-05 00:44:36
Django
NikClik, 2018-12-05 00:44:36

How to remove _state from model object instance?

I make an instance of a record from instance, there is a '_state' attribute, as soon as I try to remove it from there, an error pops up that the instance does not have the '_state' attribute, but it is, and in other models it is also there and I can easily delete it from there, namely in one model - no. How can I not insert this '_state' into the model record instance or how can I delete it correctly?
signals.py

def create_debtor_data(instance, sender, key_name):
    debtor = instance.__dict__
    print(debtor)
    debtor.pop('_state')

Mistake
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 590, in get_queryset
    return self.instance._prefetched_objects_cache[self.field.remote_field.get_cache_name()]
AttributeError: 'DebtorData' object has no attribute '_prefetched_objects_cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Program Files\Python36\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\generics.py", line 287, in put
    return self.update(request, *args, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\mixins.py", line 77, in update
    return Response(serializer.data)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 560, in data
    ret = super(Serializer, self).data
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 527, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 680, in to_representation
    iterable = data.all() if isinstance(data, models.Manager) else data
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\manager.py", line 153, in all
    return self.get_queryset()
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 593, in get_queryset
    return self._apply_rel_filters(queryset)
  File "C:\Program Files\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py", line 569, in _apply_rel_filters
    db = self._db or router.db_for_read(self.model, instance=self.instance)
  File "C:\Program Files\Python36\lib\site-packages\django\db\utils.py", line 262, in _route_db
    if instance is not None and instance._state.db:
AttributeError: 'DebtorData' object has no attribute '_state'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question