type object 'X' has no attribute 'objects'
Asked Answered
P

4

40

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()

Padus answered 21/2, 2016 at 23:53 Comment(0)
S
58

The line notifications = Notification.objects.all() is referencing the Notification View class defined in api.py and not models.py.

The easiest way to fix this error is to rename the Notification class in either api.py or models.py so that you can refer to your model properly. Another option would be to use named imports:

from .models import Notification as NotificationModel

class Notification(generics.ListAPIView):
    ...
    def get_queryset(self):
        notifications = NotificationModel.objects.all()
        ...
Stardom answered 22/2, 2016 at 0:37 Comment(0)
L
33

Add objects = models.Manager() to your model, or any other custom manager that you are using and/or define.

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)

    objects = models.Manager()
Lemus answered 17/7, 2018 at 14:54 Comment(3)
any explanation why sometimes we need to do that?Cannes
The core issue is what Derek Kwok mentioned. I missed it when I answered this. But he's right, the issue is the duplication of the Notification class name in both the models.py and the views.py. You shouldn't have to define the objects variable unless you want to define a custom manager.Lemus
In my case, apparently, I need to add the default manager because there was a custom manager, even though the name is different from "objects". ``` class MyClass(models.Model): active_objects = DeadlineManager() # only this: has no attribute 'objects' objects = models.Manager() # adding this: objects.all() works fine ``` Don't know if it is a rule for adding custom managers, but when removing both, .objects.all() works as well.Gynaecomastia
S
3

Not the answer to this question but if you have come via Google you may have accidentally marked your model as abstract and are trying to query it directly, in which case you need to remove:

    class Meta:
        abstract = True
Scientistic answered 17/9, 2020 at 5:19 Comment(0)
J
0

Registering the model with same name as the model itself

This could also be the source of the error. E.g;

# admin.py

@admin.register(Notification)
class Notification(admin.ModelAdmin):
    ...
Janot answered 18/2 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.