Default for dropdown menu in Django admin - python

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.

Related

searchable dropdown list for adding a new post in django [duplicate]

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.

Forms in Django. Drop-down list

In a django (python) project, I have a foreign key model that reflects a form to fill out. Is it possible to make it so that if the user does not find his option in the drop-down list, he has the opportunity to fill in this field, but with his own option (respectively, with saving to the database). And in the future this option will be shown in the drop-down list. Like in the picture.
So that the user can create a new item if it is not in the dropdown list.
What should be applied to implement this idea? What can be found in the documentation?
Samuel Cormier-Iijima's snippet provides a choice field with an option for the user to specify a choice not yet on the list. You would populate it using the queryset of the foreign key field, and save the new choice to that model when it posts.

Why does the foreignkey show up as a dropdown box?

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']

In Django-admin, how to add filter or search for foreign key select box?

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.

Adding an item to a django admin object creation form

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]

Categories