type object 'UserProfile' has no attribute 'objects'

BlackWhite

I have this model:

class UserProfile(models.Model):
    user = models.OneToOneField(User)

    def __str__(self):
        return self.user.first_name

and in admin:

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):

    list_display = ('name', "is_active")
    list_filter = ('name',)
    fields = ('name', "status")


    def get_queryset(self, request):
        qs = super(MyModelAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs

        user_profile = UserProfile.objects.get(user = request.user)

        if user_profile:
            return qs.filter(id = 1)

        return qs.filter(id=None)

I do not understand why object 'UserProfile' has no attribute 'objects'. How can I get object UserProfile where user from UserProfile is current user?

Daniel Roseman

You've probably defined something else called UserProfile in that file.

Nevertheless, you don't need to access it via the class; since you have a user, you can just follow the relationship with user.userprofile.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

type object 'UserProfile' has no attribute 'objects'

From Dev

'UserProfile' object has no attribute 'id'

From Dev

Django - AttributeError: 'UserProfile' object has no attribute 'urls'

From Dev

Django - 'UserProfile' object has no attribute 'get'

From Dev

Django Test: type object has no attribute 'objects'

From Dev

Django Admin - 'UserProfile' object has no attribute 'first_name'

From Dev

Django Admin - 'UserProfile' object has no attribute 'first_name'

From Dev

AttributeError at /api/test type object 'Product' has no attribute 'objects'

From Dev

'ModelChoiceField' object has no attribute 'objects'

From Dev

Python Attribute Error: type object has no attribute

From Dev

Trouble saving Django form: 'UserProfile' object has no attribute 'save_m2m'

From Dev

Trouble saving Django form: 'UserProfile' object has no attribute 'save_m2m'

From Dev

AttributeError: 'list' object has no attribute 'objects'

From Dev

Get following error when creating user in Django: type object 'User' has no attribute 'objects'

From Dev

Python/DJango Attribute error : Model object has not attribute objects

From Dev

AttributeError: type object X has no attribute Y

From Dev

type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'

From Dev

type object 'SignupForm' has no attribute 'validate'

From Dev

AttributeError: Type Object 'Ned' has no Attribute 'attack'?

From Dev

AttributeError: type object 'User' has no attribute 'query'

From Dev

Error: type object 'Keys' has no attribute 'chord'

From Dev

AttributeError: type object 'Team' has no attribute '_meta'

From Dev

AttributeError: type object has no attribute for a django model

From Dev

None type object has no attribute django tenants

From Dev

djangorestframework :type object has no attribute 'id'

From Dev

type object x has no attribute y

From Dev

type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'

From Dev

Error: Type object has no attribute 'Query'

From Dev

AttributeError type object Person has no attribute id

Related Related

  1. 1

    type object 'UserProfile' has no attribute 'objects'

  2. 2

    'UserProfile' object has no attribute 'id'

  3. 3

    Django - AttributeError: 'UserProfile' object has no attribute 'urls'

  4. 4

    Django - 'UserProfile' object has no attribute 'get'

  5. 5

    Django Test: type object has no attribute 'objects'

  6. 6

    Django Admin - 'UserProfile' object has no attribute 'first_name'

  7. 7

    Django Admin - 'UserProfile' object has no attribute 'first_name'

  8. 8

    AttributeError at /api/test type object 'Product' has no attribute 'objects'

  9. 9

    'ModelChoiceField' object has no attribute 'objects'

  10. 10

    Python Attribute Error: type object has no attribute

  11. 11

    Trouble saving Django form: 'UserProfile' object has no attribute 'save_m2m'

  12. 12

    Trouble saving Django form: 'UserProfile' object has no attribute 'save_m2m'

  13. 13

    AttributeError: 'list' object has no attribute 'objects'

  14. 14

    Get following error when creating user in Django: type object 'User' has no attribute 'objects'

  15. 15

    Python/DJango Attribute error : Model object has not attribute objects

  16. 16

    AttributeError: type object X has no attribute Y

  17. 17

    type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'

  18. 18

    type object 'SignupForm' has no attribute 'validate'

  19. 19

    AttributeError: Type Object 'Ned' has no Attribute 'attack'?

  20. 20

    AttributeError: type object 'User' has no attribute 'query'

  21. 21

    Error: type object 'Keys' has no attribute 'chord'

  22. 22

    AttributeError: type object 'Team' has no attribute '_meta'

  23. 23

    AttributeError: type object has no attribute for a django model

  24. 24

    None type object has no attribute django tenants

  25. 25

    djangorestframework :type object has no attribute 'id'

  26. 26

    type object x has no attribute y

  27. 27

    type object 'ModelDeclarativeMetaclass' has no attribute 'Meta'

  28. 28

    Error: Type object has no attribute 'Query'

  29. 29

    AttributeError type object Person has no attribute id

HotTag

Archive