1

I have a rather large CMS with many modules and am regulating access with Django permissions. Each app has a UserProfile model with a User object as a ForeignKey. I want to be able to allow easy lookup and creation of UserProfile objects within each app. I wanted to use Inlines to do it like this (from project/app/admin.py):

class InlineModelForUser(admin.TabularInline):
    model = User
    extra=0

class UserSettings(admin.ModelAdmin):
    search_fields = ['username', 'name']
    readonly_fields = ['username','name', 'otherproperites']
    inlines = [InlineModelForUser,]

admin.site.register(User, UserSettings)

Error returned is:"The model User is already registered". I understand why it's happening and I appreciate that I can probably implement the same functionality in a different way. What I am trying to achieve is segregate the editing of the base object (User) and the app profile object attached. I see of course that I can make an autocomplete field in the CreateView for the UserProfile, but sometimes you need to provide different filtering tools for each app (filtering for/against User properties) AND the need to hide some user information from the users of each app.

Basically: How can I within the django.contrib.admin build several views against the same model and register it with admin.site.register?

  • What is still missing from this is that the proxy object needs it's own set of permissions. I'm not entirely sure how you would best implement it. I'm doing it by hand for this use-case I'm solving, but will report back if I find something elegant. – Árni St. Steinunnarson Oct 09 '14 at 11:29

1 Answers1

3

Found this and it partially answers the dilemma:

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'pubdate','user')

class MyPosts(Post):
    class Meta:
        proxy = True

class MyPostAdmin(PostAdmin):
    def queryset(self, request):
        return self.model.objects.filter(user = request.user)

admin.site.register(Post, PostAdmin)
admin.site.register(MyPost, MyPostAdmin)
Community
  • 1
  • 1