Django Admin - Configuring Change List Entries - django

In Django admin, As of now, change list of a model shows 100 entries per page. Is there any configuration available to customize this limit?

You need a list_per_page attribute in your Django admin.py:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_per_page

Related

django: Make better looking multiple select menus in admin as displayed in auth>user tables

I have a User model that has these two fields:
permissions = models.ManyToManyField(Permission)
groups = models.ManyToManyField(Group)
I register the model in the admin. When I view the user model that I made in the admin section I get a multiple select menu that looks like this.
I would much prefer the much better-looking menu like the one that is in the auth user model that comes built into Django admin (looks like this)
Any ideas on what I need to do to be able to access this sort of select menu?
Thanks for your help.
Django newb from PHP (finally)
Use filter_horizontal (or filter_vertical) field in your admin class.
For example:
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
filter_horizontal = 'groups', 'permissions', 'any_other_m2m_field'
...
Also link to Django docs:
https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal

Add a custom column in django database

Iam little new to Django and Iam struggling to add column in Django Database.
I was working to create a subcategory for products, but the I want those subcategories to be predefined. That is for example "Fashion" category could have subcategory of 'Men', 'Women', 'Kids'.
I want a table in DB of these subcategories, without having the user or admin panel option to manipulate these field. User can only select article belong to which subcategory.
I go through a few documentation, but could understood much:
1. https://docs.djangoproject.com/en/3.0/howto/custom-model-fields/
2. Adding fields to an already existing database for Django (version < 1.7)
Please suggest me how to add these predefined table values.
Create subcategories model and model admin then in model admin you can disable change permission like this
Subcategories admin model
def has_change_permission(self, request, obj=None):
return False
Now you can add,delete and view only permission. User and admin can't change them.

How to edit the Django Admin User social auths listing page?

I am having trouble finding where this admin file exists so I can add an extra field. I think it's auto-magically created upon setup.
I want to add a date field, specifically, to the listing page (shown below), perhaps after the UID field so I can know when the user auth was created.
screenshot of django user social auths listing page
Okay here's what I've tried using Django-allauth and I think it somehow works the same with django-socialauth. Just get the gist of the idea and work it to your code
Extend first the SocialAccountAdmin in any of your admin.py files, better if in a specific app like "user", "home", or whatever you prefer.
admin.py
from allauth.socialaccount.admin import SocialAccountAdmin
from allauth.socialaccount.models import SocialAccount
class MySocialAccount(SocialAccountAdmin):
list_display = ('user', 'uid', 'provider', 'date_joined') # I haven't tried just adding a certain list to the list_display, for the meantime add all necessary fields just like how socialauth did
admin.site.unregister(SocialAccount) # Need to unregister the default socialaccount admin
admin.site.register(SocialAccount, MySocialAccount) # Then register it back with the custom made admin
There may be perhaps a better way to do this but this did the work.
Can it be interesting to just add a field to your model ? Adding a DateField for your creation date. Probably you need to understand learn more with : https://docs.djangoproject.com/en/3.0/ref/models/fields/

Django: How to add users from group view in django admin?

How can i add users to a group using django admin just like adding permission ?
A found an answer for older version of django
https://stackoverflow.com/a/39648244/11616789
How can i implement it in django 2 and above

Django admin custom queryset from UI

Is it possible with django to add a custom html select to an admin page in order to modify the queryset of a specific model?
For example, I have a "Product" model and inside the Products admin page I get all the product.
I would like to have an html select in that page so that the admin can change the queryset not to show ALL the products but just part of it
Thanks