BlackWhite
BlackWhite

Reputation: 832

type object 'UserProfile' has no attribute 'objects'

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?

Upvotes: 2

Views: 3897

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599590

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.

Upvotes: 6

Related Questions