문제

I have a model that looks like this..

from django.contrib.auth.models import User

class SampleModel(models.Model):
    info1 = models.CharField(max_length = 20)
    info2 = models.CharField(max_length = 20)
    objectAdmin = models.ForeignKey(User)

django by default gives access to users to the entire tables if you allow them, but I want the user that is in that row to be the only one to have access to the entry..

so when a particular user logs in, and he goes to the /admin/myapp/samplemodel/1/ ,he should only be able to edit that object if he is the "objectAdmin" of that entry..

도움이 되었습니까?

해결책

Filter the queryset in admin based on the user logged in.

class SampleModelAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(SampleModelAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(objectAdmin = request.user)

This will ensure that the user has access to objects for which he is assigned as admin.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top