Hi Just wondering why when I try to include a foreign key in my model it shows up as a dropdown box rather than a textbox?
Here is my models.py
class VolunteerApplication(models.Model):
volunteer_name = models.ForeignKey(VolunteerProfile, on_delete=models.PROTECT)
phone_number = models.CharField(max_length=10)`
This is what it shows up as on the website
This happens because its a foreign key. This is default rendering behaviour of the django admin.
The admin screen slows down/refuses to load if there are too many objects that the admin is trying to render in the dropdown.
The problem with raw_id_fields is that it doesn't show the string representation of the selected foreign key. An easy way to see the preview of foreign key without the dropdown - use django-dynamic-raw-id package.
https://github.com/lincolnloop/django-dynamic-raw-id
In your admin form set your field as a raw_id_fields
class AdminForm
.....
raw_id_fields = ['volunteer_name']
Related
I am creating a manytomany field in my model and creating a Inline Tabular with though also. but on django admin it is showing dropdown. and if I add new then it shows a Popup. But I dont want Popup. Instead of that I want inline add as in case of Foreign Key.
Thanks in advance. Please help...
class ExamInline(admin.TabularInline):
# classes = ['collapse']
model = Form.exam.through
extra = 0
For establishing many-to-many relationships, django admin has a nice widget called 'filter_horizontal'.
The problem is - it only displays primary key of the related table.
How do I add other fields from the related table?
For example, if I have a many-to-many relationship in my Order model with User model, in 'Order' django admin I can only see User's primary key(id). How do I add their name into the widget?
Turns out it is changed in str method for the User model
I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.
I have a django model, NewsItem, which has several fields, including date, text, and foreign key fields. One foreign key field is:
editor = models.ForeignKey(User, verbose_name="Editor", related_name='editors',
limit_choices_to=_editors)
For some reason, the foreign key fields do not show up in the "add an item" form in the django admin interface (at http://[hostname]/admin/[app name]/newsitem/add/. However, all the other fields do. I can't save any items because editor is a required field.
I have checked to make sure that there is a user satisfying the constraint:
>>> User.objects.filter(groups__name__iexact='editors')
[<User: testeditor>]
I can't find any reason that these fields wouldn't show up in the admin. Do I need to specify a widget for them in the NewsItemAdmin class? Do I need to tell the admin to display them? If so, how?
You should make sure the user you're creating this with has permission to change the editor. [facepalm]
I'm using Django 1.6. I am trying to work out whether there is a way to set a default for the option that appears in a dropdown box on the admin site (rather than the blank ------ option that appears). Specifically I am working with dropdown boxes for foreign keys, so they have no "choice" field.
Dropdown default can be set by specifying it in the foreign key field in models.py :
foreign_key_field = models.ForeignKey("keyname", default=1)
This will select the record with id=1 in the corresponding admin dropdown by default. You may change default=1 to any id that you want to show.