AttributeError:对象没有属性“ _state”

时间:2018-09-25 02:01:35

标签: python django django-models django-views

我正在开发这个django应用,并且我一直在收到这个AttributeError。

这是我的 models.py

class Service(models.Model):
    slug = models.SlugField()
    provider = models.ForeignKey(User, related_name="service_provider", on_delete=models.CASCADE)
    description = models.TextField()

这是我的 views.py

from django.contrib.auth import get_user_model
User_s = get_user_model()


class ProfileServices(LoginRequiredMixin,ListView):

    template_name = 'users/manage_listed.html'
    queryset = User_s.service_provider
    context_object_name = 'service_list'

在html模板中,其调用方式如下:

{% for services in service_list.all %}

我得到的错误是'ProfileServices'对象没有属性'_state'

任何帮助将不胜感激! 谢谢!

使用完全追溯功能编辑整个错误消息:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/users/3/owned/

Django Version: 2.0.2
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'debug_toolbar',
 'bootstrap4',
 'users',
 'widget_tweaks',
 'flats',
 'haystack',
 'bugreport',
 'django_messages',
 'services',
 'subscriptions']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware']


Template error:
In template C:\Users\yk\onyx-main\My_site\mysite\templates\base.html, error at line 0
   'ProfileServices' object has no attribute '_state'
   1 : <!DOCTYPE html>
   2 : {% load staticfiles %}
   3 : <html lang="en" dir="ltr">
   4 :   <head>
   5 :     <!-- BOOTSTRAP -->
   6 :     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
   7 :     <!-- FONT AWESOME ICONS -->
   8 :     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
   9 :     <!-- GOOGLE FONTS -->
   10 :     <link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">


Traceback:

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\related_descriptors.py" in get_queryset
  562.                 return self.instance._prefetched_objects_cache[self.field.related_query_name()]

During handling of the above exception ('ProfileServices' object has no attribute '_prefetched_objects_cache'), another exception occurred:

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  158.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  156.                 response = response.render()

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\response.py" in render
  106.             self.content = self.rendered_content

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\response.py" in rendered_content
  83.         content = template.render(context, self._request)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\backends\django.py" in render
  61.             return self.template.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render
  175.                     return self._render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\test\utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\loader_tags.py" in render
  155.             return compiled_parent._render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\test\utils.py" in instrumented_test_render
  98.     return self.nodelist.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\loader_tags.py" in render
  67.                 result = block.nodelist.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render
  943.                 bit = node.render_annotated(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in render_annotated
  910.             return self.render(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\defaulttags.py" in render
  161.                 values = self.sequence.resolve(context, True)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in resolve
  676.                 obj = self.var.resolve(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in resolve
  802.             value = self._resolve_lookup(context)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\base.py" in _resolve_lookup
  864.                             current = current()

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\manager.py" in all
  155.         return self.get_queryset()

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\related_descriptors.py" in get_queryset
  565.                 return self._apply_rel_filters(queryset)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\models\fields\related_descriptors.py" in _apply_rel_filters
  541.             db = self._db or router.db_for_read(self.model, instance=self.instance)

File "C:\Users\yk\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\db\utils.py" in _route_db
  262.             if instance is not None and instance._state.db:

Exception Type: AttributeError at /users/3/owned/
Exception Value: 'ProfileServices' object has no attribute '_state'

1 个答案:

答案 0 :(得分:0)

终于可以使用了!

这是 Views.py

class ProfileServices(LoginRequiredMixin,UserPassesTestMixin,ListView):

    def get_queryset(self):
        user_t = User.objects.get(pk=self.request.user.pk)
        return user_t.service_provider.all()

    template_name = 'users/manage_listed.html'

    context_object_name = 'service_list'

我猜测问题出在我正在使用的 get_user_model()方法。