As another option, you can do look ups like:
class UserAdmin(admin.ModelAdmin):
list_display = (..., 'get_author')
def get_author(self, obj):
return obj.book.author
get_author.short_description = 'Author'
get_author.admin_order_field = 'book__author'
Since Django 3.2 you can use display()
decorator:
class UserAdmin(admin.ModelAdmin):
list_display = (..., 'get_author')
@display(ordering='book__author', description='Author')
def get_author(self, obj):
return obj.book.author
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)
Best Answer
Replying to own question may seem a bit odd but I found another solution ;)
There was a problem to provide custom queryset to a formset, there is no hook in case of inline formsets for this. So I subclassed BaseInlineFormSet and overridden the get_queryset method. Then I just provide this formset in InlineModelAdmin and it's done.
Example:
and admin class: