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
Related
I have objects and catetories in Django and object has ForeignKey to Category.
In Admin user must first create categories and then create objects choosing category from list.
What I want to have is file manager metaphore: User opens list of categories, clicks "create object here" and creates one like we do it with folders and files.
I wonder if Django has some application for it. Does it?
I know about inlines, but I do not want to edit object on the same page, I want to have list of them.
It is similar to Django: adding an "Add new" button for a ForeignKey in a ModelForm but I do not have ModelForm, I speak about admin
In Django admin, you can use inlines, example:
class OBJECTSInline(admin.TabularInline):
model = OBJECTS
extra = 0
class CATEGORYAdmin(admin.ModelAdmin):
list_per_page = 10
inlines = [OBJECTSInline,]
admin.site.register(CATEGORY, CATEGORYAdmin)
If you register both your models in the Django admin, you will see that when creating/editing an object with foreign key to Category, there will be a dropdown for the foreign key value, which lists existing values, and next to it, buttons for editing/deleting the selected value, or for creating a new Category:
That seems to be what you're asking for - it's already there in the admin as soon as you register the relevant models.
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.
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.
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"})
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.