Multiple Modeladmins/Views for Same Model in Django Admin

Multiple ModelAdmins/views for same model in Django admin

I've found one way to achieve what I want, by using proxy models to get around the fact that each model may be registered only once.

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

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

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

admin.site.register(Post, PostAdmin)
admin.site.register(MyPost, MyPostAdmin)

Then the default PostAdmin would be accessible at /admin/myapp/post and the list of posts owned by the user would be at /admin/myapp/myposts.

After looking at http://code.djangoproject.com/wiki/DynamicModels, I've come up with the following function utility function to do the same thing:

def create_modeladmin(modeladmin, model, name = None):
class Meta:
proxy = True
app_label = model._meta.app_label

attrs = {'__module__': '', 'Meta': Meta}

newmodel = type(name, (model,), attrs)

admin.site.register(newmodel, modeladmin)
return modeladmin

This can be used as follows:

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

create_modeladmin(MyPostAdmin, name='my-posts', model=Post)

How can i have two ModelAdmin of the same model in Django Admin

Perhaps you can use proxy models
Like..

class MyStudent(Student):
class Meta:
proxy=True

class MyStudentAdmin(ModelAdmin):
list_display = ('id', 'user', 'created')
search_fields = ('username',)

site.register(Student, ModelAdmin)
site.register(MyStudent, MyStudentAdmin)

Django, multiple ModelAdmin for one Model

Create a proxy model - that will allow you to register it in admin as a different model, but it will be still the same:

class MyModel(models.Model):
pass

class MyModelAgain(MyModel):
class Meta:
proxy = True

admin.py:

admin.site.register(MyModel)
admin.site.register(MyModelAgain)

Django Admin: Register multiple admin classes to the same model

Turns out I didn't need to make a proxy model. I fixed the issue by adding the prepopulated_fields variable to the MyPostAdmin class

two admin classes for one model django

Very close! What you want is to add a proxy model in your models.py:

class ItemPending(Item):
class Meta:
proxy = True

And then register the second ModelAdmin like so:

admin.site.register(ItemPending, ItemAdminPending)

Multiple ModelAdmins for one Wagtail model

Use a proxy model and then define an appropriate manager for each proxy model. I have this working in an existing Wagtail-based application where I define proxy models for various states of Memberships in a membership application. In my case the base model is Member, but then I have CurrentMember, NonCurrentMember, etc. This comment and related discussion might also be of interest.



Related Topics



Leave a reply



Submit