how to make a data fetch form in django? - python

I want the user to have a form for fetching data from the database, for example, as in /admin:
Form in admin
So that the user can select multiple entries and, for example, delete them, how can I do this?

if you want to modify the django admin and add the functionality you need to use this package from django.contrib import admin to register your personal models by extending admin.ModelAdmin
To change the admin interface you need to override template files in .../site-packages/django/contrib/admin/templates/ Here is the full tutorial for more clarification
https://realpython.com/customize-django-admin-python/

Related

Setting an option that can only be viewed by users in a certain group

I am quite new to django and am building a project using django and python predominantly.
I have two user groups in djangos admin panel created and defined there with user added through this admin panel;
Diving_Officers and Club_Members
In my webpage, i have an option that i want to only be visible or even clickable to the users in one group, the Diving_Officers group.
I cannot find any specific information for how to call a group that exists in django admin and assign it permissions or how to limit a view to it.
**in short how do i add a set of permissions to a group, ie. people in this group can only view this page
On a view you can use the decorator #user_passes_test() (you can find more informations here https://docs.djangoproject.com/en/4.1/topics/auth/default)
to make the option visible only to user in that group instead you can use a template filter, create a "templatetags" folder in your app and add this into "has_group.py" in that folder:
from django import template
register = template.Library()
#register.filter
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
then, after loading in the template with "load has_group" you can use user|has_group:"mygroup" as if condition

Django create a custom form permission

I'm developing a management software. And I need create a module for manage the permissions and groups using the auth of django. I dont want use the admin django because this just allow log in for super users.
I want override the admin route and create a form with the same features from the admin site. If is possible, I want use the widget for the assignment of permission and group.
I need all this built into an app because I need this to work for this and other projects.
I have already written a custom form to add, edit and view users extending the class UserCreationForm, I need something similar to that.
I hope you can help me...
First things first: don't do this!
Creating your own Django admin site is a load of work, and likely to be insecure etc. Your'e opening a giant can of worms here.
If you need members of your app to edit permissions, they do not have to be superusers! Users with is_staff = True can all access the admin site. Once you've set this for the users you want, go ahead and configure the exact permissions for this type of user.
Start with the official docs on user permissions.

django admin custom template for model redirection

A curious question
Suppose I have a module/app say Demo which contains a model say "anything". Now I registered this model to admin.py.
Now when I log in to admin and see this model I see general update, delete option in admin template.
Is it possible that when I see this model in admin and click that model it redirects to another url that is of that module's url and it display in another template and I can show anything I like from view..
I am sorry if it is confusing. What I want is I want to show the admin's model property like update, add and delete in another custom template made by me and it should be redirected to url defined in the module's urls.py
Thanks in advance.. :)
This can be done by extending your base get_urls() method docs here

Is it possible to extend Django admin with views not related to the database tables content?

I work on a simple app with two basic models, Document and Word. The user uploads a document, and each word from the Document is extracted in a Word entry. The user should be able to view, add, edit and remove the documents and the words, so I thought it would be good to use Django admin. But also the user should be able to do something more with the words, usually in a group of two and some additional adjustable parameters (like calculating Dice similarity).
Is there a way I can do all this in a one-level application? Or I have to make every user admin user, and make an app with just the additional functionality?
You can create custom view in the ModelAdmin or even in the admin site so there is no need to create an additional app.

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