replace Add with Get on django admin site - django

I have a model that I don't want user to manually add/delete objects. Instead, I want to have some Get button that once clicked, it executes some code and automatically update the database.
I was able to do similar things via adding my own admin action. However, adding an admin action by default means you have to have at least one object for your model, you select that object and performs some action.
I want be able to perform my customized action on an empty model (if I can replace the Add button with a Get button on the index page, it will be perfect). Is there a way to do so? thanks!

You could override the add_view method of the django admin model and do whatever you felt like having that view do if you wanted. Poke around through the ModelAdmin code and you can see what it does by default
you could also take a look at this project: https://github.com/imtapps/django-admin-ext and see an example of how to register your own urls for an admin... so you could make your view something like admin/myproject/myapp/model/get and have it do what you'd like...

You can prevent users from adding new objects by removing the "Can add ModelName" permission from that user. If they're a superuser that won't actually stop them from doing that, however.
Admin actions are designed to be run on specific instances of the model; if that's not what your code is doing you probably shouldn't go that particular route. You can override the admin template for your model and add a new button or link to your custom view. You could also remove the Add button entirely by eliminating it from your template, but I'd recommend using permissions instead.

Related

Django Admin LogEntry: how it works in non admin actions?

I am having some struggles how does exactly django.admin.LogEntry objects are created.
Consider the following scenario:
I have a bunch of functions which take a csv file with data that allow me to create multiple objects at one call (just iterate through the file, use the data and if data in given row is correct: create a Model instance). I want to make sure that that each of that creation will be logged.
The question is: django docs are not very descriptive on how does LogEntry works and I am not sure if such actions (not taken in the admin panel itself) will be logged there. Also: will the LogEntries be created for the related objects or I have to trigger them manually?
Does anybody got any experience with such scenarios and can share thoughts about it?
The LogEntry model is in the Admin package and only used by Django admin by default. It is used in the admin layer and not model layer when saving objects. if you want to use it outside the admin, then you will have to manually create the entries yourself. That also means the admin will likely display entries of changes made by normal users so you have to think about how you want the entries displayed

Adding record of page views and details of change to django_admin_log

We are using LogEntry/django_admin_log and recording additions, changes and deletions made from Django Admin. However we have two issues we need to address:
1.) Changes record only the field that changed. Not the old and new values. We would like to add the specific details of all changes.
2.) We would like to record an action in this log every time a page is viewed on the Django Admin panel.
How would it be best to proceed?
We are happy to do some work to extend the existing functionality of this, or we are happy to move to a completely new 3rd part module if necessary, or write our own. But would like some guidance from some experts?
We had similar requirements in terms of keeping history and track of actions done by users with a higher level of detail in terms of values changes. What we ended up doing was creating a new model called "History" which we populated with the user, the name of the model being changed, the instance id and a dictionary called changes showing the name of each field changed and values from - to.
In order to populate the new model, we overrode the save_model function in the admin file for the model we want to track. Regarding the page views, you can overrride the get_fields if "obj" is not None and create an instance of History accordingly.

How to let user create a page via a URL?

I am thinking along the lines of /mysite/blogs/new. I know about add_child and about Routablepage, also the 'permissions route' but I still don't see how to permit page creation via a URL.
I am surprised that there do not appear to be any instructions for doing this, as relying on the admin method surely isn't acceptable for many 'naive' users. Indeed, it took me a while to realise that Wagtail isn't like Wordpress, in the sense of having an 'Add Post' button or the like.
The documentation does not clearly state that users are (apparently) expected to create pages via the Admin section either. In fact, there seems to be an assumption that developers will be familiar with Django, even though a CMS ought to be easier to use IMO.
Just to be clear, I think that Wagtail is great but the instructions leave something to be desired. Perhaps I will attempt to update them myself!
If you have the Edit Bird/Userbar enabled, then it presents a button to add a child page at any point in the page tree so long as you have permission to add one there. If you wish to give naïve users this option then you could make their permissions fairly restrictive, so that they only see this option at certain locations, and they don't see any of the other 'settings' areas of the Admin.
The admin page for adding this does have its own URL: it typically looks like http://www.example.com/admin/pages/13/add_subpage/, where 13 is the ID of the current page. If instead you want to present that link yourself, you can manually add the link in the template as
Add a child page
but first you should ensure that the user has the relevant permission. This is best done in Python, rather than in the template. See https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailadmin/userbar.py for how Wagtail does this.
To get closer to your original request, you could combine all the above with RoutablePageMixin, by creating a route 'add' which redirects to the same URL:
#route(r'^add$')
def add_child_page(self, request):
return redirect(reverse('wagtailadmin_pages:add_subpage', args=[self.pk]))

django admin panel add a confirmation window

I need to add a confirmation window on admin change model submit "save". Smth like this: "Do you really want to apply changes? Yes/No". I need it only for one model and only if the changed model satisfy the other conditions. I can`t override django template for one model cause of using django-admin-tool. How can i do this ?

Redirect a custom form to django admin form

I created a custom admin form for a particular model. All functionalities working. Now I want to call this form from the click of a button on another page that I have created. I was able to do that by creating an object of the form class, but it doesn't render the form as the admin site does (I had some fieldsets etc defined). Can I straight away redirect to the admin site's form? That would avoid duplicating the code for how I handle the form data. Any other leads would also be appreciated.
Now I want to call this form from the click of a button on another page that I have created.
Is this also within the admin interface? If so, don't use the admin view for all your users. As that will require nasty stuff with groups, permissions, etc.
I was able to do that by creating an object of the form class, but it doesn't render the form as the admin site does (I had some fieldsets etc defined).
Isn't this as expected, as the admin site comes with its own css and javascript? So you need to write it yourself
Can I straight away redirect to the admin site's form? That would avoid
duplicating the code for how I handle the form data. Any other leads
would also be appreciated.
True, but I believe the admin views have special authorisation checks on them. So you come back to the things with groups and permissions.
What I would do is move your form handling to an method in like utils.py and call this method from the normal and admin view.