Adding an item to a django admin object creation form - python

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]

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.

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

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.

Filter a User list using a UserProfile field in Django Admin

I'm trying to filter the User list in Django using a UserProfile Field... I need to implement a queue system where new users are put in a queue until an Admin approves them.
I simply added a is_in_queue boolean field to my UserProfile model... However, when displaying the user list in my Admin area, I realized that you can't filter the list using a Model's foreign key field (in this case, a field of UserProfile)
Apparently, list_display items can be callables but list_filter can't, so I can list IF a user is in the queue without a problem, but the admin would have to scroll through the whole user list to spot which ones are in the queue which makes no sense... Filtering only users that are in the queue (using userprofile.in_queue) would be much more practical...
Finally, I thought about adding a custom view to my admin area that would list only the user in the queue, but that custom view does not show up on the Admin area Index page, and putting together a whole new AdminSite only for a new filtering option seems a bit over the top...
So basically to sum it up: Can I filter my User list based on a
UserProfile field? If not, can I add a custom view that's accessible
from the front page without having to create a completely new
AdminSite only for that?
Django 1.3 fixed that - list_filter now allows to span relations:
https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
You may want to take a look in to using a custom manager for the admin_objects of your model.
class UserAdminManager(models.AdminManager):
"""
Custom manager for the User model.
"""
def get_query_set(self):
"""
Overwrites the get_query_set to only return Users in the queue.
"""
return super(UserAdminManager, self).get_query_set().filter(userprofile__queue=True)
By overwriting the get_query_set method you can filter the results. Then just assign this to the admin_objects property of your User model.
admin_objects = UserAdminManager()
Some of the property names in my example may be wrong, as I don't know your model setup, but hopefully you get the idea.
You can research this further by checking out the django docs and searching for "custom managers".
It sounds to me like the quickest and easiest option is to add a new admin view to your application, specifically for your custom user model. See the Django admin docs for details, though it sounds like you know how to use Admin already.
Once the admin page is specific to your model, all your custom fields will no longer be foreign keys. This would make filtering easy.

Categories