Django admin custom queryset from UI - django

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

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.

Django how to change the admin panel template

I am trying to change what is displayed when I click on a model in the django admin. For example if I have the model Book and then I go into the admin panel and click book, it will give me all the fields that I created the model with.
But I want to edit that page that shows all that data. For example, if I click on the "Book" model in the admin panel and see my fields to edit, I want to put another bit of fields on the bottom from another model. Or maybe I want to put like a "Similar Books" list. Is it possible to edit this page?
Admin customization information is here. You can do stuff like list_display or fieldsets etc.

How can I update a model on the Django admin page when the model is click to be viewed?

I have a function that updates my Orders model. In my admin.py, I register the model, and have a admin.ModelAdmin class associated with it. I want to call a function that runs when the registered model is clicked on the admin page, so that it updates before the users sees any information about the orders. I've tried just adding the function:
class OrdersAdmin(admin.ModelAdmin):
...
update_orders()
But this just results in it being updated when I run the server, and not when the "Orders" model is clicked in the admin. I've looked into admin actions, but that is only related to making changes to various fields of the model after the page is populated with objects from Orders Model.
How can I pass a function to be called when I click on the model in the admin page? Thanks.
I guess the best way to do this is to override the get_absolute_url method in your model See docs.
def get_absolute_url(self):
#update you orders here .....
from django.urls import reverse
return reverse('admin:yourapp_yourmodel_change', args=[str(self.id)])

Order items alphabetically in Django admin site's multi-select box

My team's having a lot of trouble finding items in Django admin site's multi-select drop-downs. How do I order items in multi-select drop-downs alphabetically?
Try doing that with the form that you are entering on Form Admin
from django import forms
class YourForm(forms.ModelForm):
attribute = forms.MultipleChoiceField(queryset=Model.objects.order_by('name'))
class Meta:
model = YourModel