Django Admin: group by foreign key - django

Lets say I have model Book that has foreign key to model Author, now I would like to modify Django admin to list the books like this
Some Author
book 1
other book
some other author
his first book
his last book
Is this possible in the admin interface?

If inlines will not suffice, then I think your only option is to create a custom admin view. Possibly use django-extra-views to handle multiple formsets there.

Related

django admin search by foreign key in add form

In the form to add new objects in Django admin, sometimes there are linked foreign keys that are selectable in a dropdown. Is there a way to include a searchbox for the foreign keys within the form?
For example, let's say I have a Book model with a foreign key to Author object. Instead of scrolling through all the author ids when I create a new Book object, can I search for Author "James" specifically?
Use autocomplete fields in your custom admin.
Django Documentation - Autocomplete Fields

Django Arrayfield of model vs ManyToManyField

Let's say I have 2 models, User and Book. User can have multiple books and book can belong to multiple users.
I only care to get what books each user has, and not so much about the other way around.
I am thinking about 2 solutions here: ManyToManyField and ArrayField of Book model.
What are the pros and cons? When I store ArrayField of Book model in User table, does Postgres duplicate the data in both Book and User table?

Django admin foreign key field filtering

I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.

Django, filter users by group in a model foreign key

I have a model for a blog post where the owner of the post is a foreign key to User. With that model any user can own a blog post. I would like to change it so that only the users in a certain group -let's call it 'bloggers'- can own a blog post object. Ideally it should appear in the admin too, I mean in the blog post admin right now the menu for 'owner' lists all the users, it should only list the ones in the 'bloggers' group.
How do I do that with Django 1.3?
Use limit_choices_to paramether in your ForeignKey definition like this:
author = models.ForeignKey("auth.User", limit_choices_to={'groups__name': "bloggers"})

displaying list of registered user in django-admin

My Book model has an author attribute which today is simply a CharField. The value for author should be one of the registered users of my Django site. When creating a new Book object in Django admin, I would like author to be displayed as a combo box showing all registered users. How would I go about achieving this?
You can make your author attribute a foreign key to django.contrib.auth.models.User and use limit_choices_to.