Type object has no attribute objects ошибка

I am using Django and Django Rest Framework 2.4.0

I get the Attribute error type object 'Notification' has no attribute 'objects'

models.py

class Notification(models.Model):
    NOTIFICATION_ID = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, related_name='user_notification')
    type = models.ForeignKey(NotificationType)
    join_code = models.CharField(max_length=10, blank=True)
    requested_userid = models.CharField(max_length=25, blank=True)
    datetime_of_notification = models.DateTimeField()
    is_active = models.BooleanField(default=True)

serializers.py:

class NotificationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Notification
        fields = (
            'type',
            'join_code',
            'requested_userid',
            'datetime_of_notification'
        )

api.py:

class Notification(generics.ListAPIView):
    serializer_class = NotificationSerializer
    def get_queryset(self):
        notifications = Notification.objects.all()
        return notifications

Can anybody help me to figure this out? It fails in api.py at the line notifications = Notification.objects.all()

fragment of models.py

class Hardware_type(models.Model):
    type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
    description = models.TextField(blank = True, verbose_name="Description")
    slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")

class Software_type(models.Model):
    type = models.CharField(blank = False, max_length = 50, verbose_name="Type")
    description = models.TextField(blank = True, verbose_name="Description")
    slug = models.SlugField(unique = True, max_length = 255, verbose_name = "Slug")

and now

>>> sw = Software_type.objects.get(slug='unix')
>>> sw
<Software_type: Unix>
>>> hw = Hardware_type.objects.get(slug='printer')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Hardware_type' has no attribute 'objects'

I can’t see why this happens. Anyone can help me?

Edit:

sorry that did not sent all the code — problem solved.
in another class I had

hardware_type = models.ManyToManyField(Hardware_type, verbose_name="Hardware Type")

after change from hardware_type to hw_type — works fine
I did not know that can cause this problem.

i run into this error when trying my hands on django-shopping-cart

error

AttributeError: type object 'Grocery' has no attribute 'objects'

the error was pointing this below

 return Grocery.objects.all()

Models.py

from django.db import models
from django.urls import reverse


class Grocery(models.Model):
    item_name = models.CharField(max_length=50)
    price = models.FloatField()
    picture = models.ImageField(upload_to='pictures', default='')

    def __str__(self):
        return self.item_name

    def get_absolute_url(self):
        return reverse('marketing_sys:home')

views.py

from django.shortcuts import render, redirect
from django.views.generic import  DetailView
from cart.cart import Cart
from .models import Grocery


class CartAdd(DetailView):
    model = Grocery
    context_object_name = 'grocery'
    template_name = 'mismas/cartdetail.html'

    def get_queryset(self):
        return Grocery.objects.all()

urls.py

from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from mismas.views import  Grocery


app_name = 'marketing_sys'


urlpatterns =[ 
   path('cart_add/<int:pk>/', CartAdd.as_view(), name='cart_add'),
]

i will be glad to receive help. thanks

I’m trying to implement my own serializer and view to handle Token based authentication with email instead of username. In copying the ObtainAuthToken view, an error is returned about the Token object not having the objects attribute.

Steps to reproduce

  1. mkdir restframework
  2. cd restframework/
  3. virtualenv env
  4. source env/bin/activate
  5. pip install django
  6. pip install djangorestframework
  7. django-admin startproject tutorial
  8. cd tutorial
  9. python manage.py shell
from rest_framework.authtoken.models import Token
Token.objects.all()

Expected behavior

Token class can query objects

Actual behavior

python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from rest_framework.authtoken.models import Token
>>> Token.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'Token' has no attribute 'objects'

This class (being a django model) should be able to query using the objects attribute, should it not?

Solution: add in Django setting

'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': False,
    'UPDATE_LAST_LOGIN': False,

For example:

SIMPLE_JWT = {

     'ACCESS_TOKEN_LIFETIME': datetime.timedelta(days=7),
    'REFRESH_TOKEN_LIFETIME': datetime.timedelta(days=7),
    'ROTATE_REFRESH_TOKENS': False,
    'BLACKLIST_AFTER_ROTATION': False,
    'UPDATE_LAST_LOGIN': False,

}

Read More:

Понравилась статья? Поделить с друзьями:
  • Type mismatch vba word ошибка
  • Type mismatch vba excel ошибка runtime 13
  • Type mismatch in expression access ошибка
  • Txd workshop ошибка floating point
  • Txd workshop ошибка access violation at address 00000000