In Django-admin, how to show details for manytomany relationship - python

In django-admin, It's not convenient to edit a manytomany field in a new pageļ¼Œbesides this, I wanna show more details in edit_inline area as foreignkey field does. What should I do to achieve this needs
This is foreignkey field:
and this is manytomany field:
can I edit manytomany field in this page? At least show some column in this area.

For the manytomany try using
(in admin.py)
filter_horizontal = ('fieldname', )
about the foreignkey, create a form for that inline and do something like...
forms.CharField(label='',required=False,
widget=forms.Textarea(attrs={'style': 'height: 20em;'}))

Related

Django admin many-to-many - how to show additional fields?

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

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

Include auto generated primary key in Django ModelForm fields

I have a model where I didn't specify a primary key and Django generated one for me. Now I create a ModelForm for the model and I have specified id in the fields section of ModelForm. However, in my ModelForm object, the id field is not present.
Are only the model fields explicitly declared visible in the ModelForm?
Are only the model fields explicitly declared visible in the
ModelForm?
Yes, generally you don't want to mess with this field, if the user inputs a value for the id field it's very likely to be duplicated so this is something you want django to take care of for you.

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]

Grouping OneToOne Fields together in the Admin interface

I have 4 OneToOne relations to a model, and in the admin area I would like to group them so they can be edited in pairs(2 groups of 2). Is there a way to do this similar to using fields?
You can specify the field order (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields) and also create fieldsets (https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets) for the admin interface

Categories