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
Related
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.
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']
I have written an own panel that utilizes the django modeladmin. The modeladmin is connected to another model via a foreign key, but I dont want the user to be able to change the foreign key relation, only the other data on the form. I have therefor tried to hide the widget via the panels argument on the model admin:
panels = [
FieldPanel("event_detail", widget=HiddenInput),
FieldPanel("title"),
FieldPanel("content"),
FieldPanel("send_time"),
]
This handles the input field correctly in the modeladmin, but sadly I still see the label in the modeladmin, which will confuse my users. How can I make sure that not only the field is removed but also the label?
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 have a Django application where users have additional data. That data is collected in a Profile model with a OneToOneField pointing to User.
This is fine and works perfectly for most purposes, but I have trouble customizing the admin for User. In particular:
I would like to be able to show a Profile field inside list_display. I don't know how to do this without writing an additional method on User itself.
I would like to be able to show some information about related models (e.g. some resources owned by the user) inside the User detail page. Again, I do not know how to do this without writing a custom User method.
Do you know any solution to the above?
You only have to edit the admin classes in admin.py. You can use admin.inline* class to help you. Example from Django website that will add Book to the Author's admin page:
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
admin.site.register(Author, AuthorAdmin)
Read more here.
EDIT: You should be able to add methods on UserAdmin model and refer to them when setting the list_display fields:
list_display = (..., 'your_method')
Turns out, one can put the methods in the UserAdmin itself instead than in the User model. This way I can access all the information I need about the user.