Add custom action to Django inline object on the admin interface - django

I have an admin interface that has a blog post, with inline models which are previus versions of the post.
I'd like to add an action for each one of the previous version (A revert action, custom model method)
how should I go about doing that?
its kinda similar to ModelAction actions keyword, but I want it to be inside the model view, not the list view
and also its for each inline model, not for the parent model
would love some help.
to make it clearer
my previous_version class has a function named revert. all I want is that in my blog post's view in the admin panel by each previous version I'll have a link or button or something. and pressing it will call previous_version.revert.

I guess the right thing to make this is Admin actions as described in documentation -
https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

You can extend Blog ModelAdmin with action revert.
Overriding inline model template to add a button, like you said you already did is a good way to do it.
Just be sure to wrap created view within admin_view and allow only post requests.

Related

Hide model from main admin list, but allow creation in inline editor

In my Django app, I have an Attribute model which has a many-to-many relationship to a MeasurementMethod model.
I put an inline for MeasurementMethod in the admin interface for Attribute, but I don't think it is useful to have a separate interface for managing MeasurementMethods at all; there's no reason why a user would say, "Gee, I wonder what Attributes can be measured by water displacement."
However, this left no way to create new MeasurementMethods from the inline editor until I found Anton Belonovich's post, which says that I need to admin.site.register(MeasurementMethod) first. I did that, and sure enough the edit and create buttons appeared.
But now on the admin page, where there's a list of apps and the models that can be managed, there's an entry for MeasurementMethod that I don't want.
Is there a way to get rid of it? Or is there a better way to accomplish this?
The solution is to register the MeasurementMethod class with a custom admin class that overrides has_module_permission:
#admin.register(MeasurementMethod)
class MeasurementMethodAdmin(admin.ModelAdmin):
def has_module_permission(self, request):
return False
Then the class can still be edited inline.
ModelAdmin.has_module_permission(request)
Should return True if displaying the module on the admin index page and accessing the module’s index page is permitted, False otherwise. ... Overriding it does not restrict access to the add, change or delete views ...
You could create a custom admin site docs and then override the index method/view. Make sure you register your models with this new admin site and hook it up in the urls.py file.

Adding an hyperlink to another Model inside Django Admin

I have something which sounds trivial but I cant find a way to implement in django admin.
I have a Model which I am showing in my admin. I am using a TabularInline to show another model which refer to the first one.
All I want is a link to click on my current view so I'll go into the TabularInline view only.
The reason I need it is simple. My second model admin registertion also contains another TabularInline to a third model. so I cant see the Inline from the first model.
The Models are pretty simple. I am implementing a Voting system over several cretirias. So I want my Item model admin to show all the users which votes and the final score. But the Vote model breaks down the cretirias that the user votes for.
This answer discusses this in some detail:
Django InlineModelAdmin: Show partially an inline model and link to the complete model
But this is what I use:
https://gist.github.com/3013072

Extending django modelform with selected action-items

In my application, I have a set of players who may be a member of (at most) one team. I am implementing a custom action in the player admin view where I select players and create a new team with those players, following the example at: www.jpichon.net/blog/2010/08/django-admin-actions-and-intermediate-pages/
However, I'm sensing that there should be a less django-intrusive way of implementing this than using a fully customized template. What I'm thinking is: can the normal ModelForm for the Team class be extended to accept arguments when coming from the create team action? I struggle to find some documentation on the subject though.
This is a screenshot of what I have so far (following the above guide):
Which I would like to result in something like:
The first part works fine when using the custom template, but I cant figure out how to (nicely) extend the existing admin page for Teams to accept the player arguments from the actions. I suppose the third step (after clicking the 'Create Team' button) can be accomplished by overriding the save() method of the Team Modelform?
Thankful for any assistance!

Django-admin with callable functions

Django-admin view allows only for changing the values of model objects. Is it, however, possible to configure or change in an easy way the admin view so that it starts exposing functions on objects? I'm not talking about the functions that can be introduced in the drop-down menu on top of the object list. What I mean is a direct access to functions on model objects?
You can add your own view to the admin site by adding an "^admin/..." url in your url conf. You can use this to extend the admin site relatively easy and expose model methods through your own view. See Creating Custom Admin Views here: http://www.djangobook.com/en/1.0/chapter17/ (and another approach and notes here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites ).
To add this as a button in the model's "change form" in the admin site, override the change_form.html template for the required models (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates ).
You will probably want to override the object-tools block, which holds the buttons in the top right side of the page. (In Django 1.3 you can extend the object-tools-items block instead, see: https://code.djangoproject.com/ticket/12694 )
(I am quite sure one can build a nice plugin/app that automatically adds object-tools to a model from a custom "object_tools" property in the ModelAdmin with a list of model methods. Let me know id you find something like this.)

Create/update a sub-object in the Django admin edit dialog

I'd like to create/update a "sub-object" within an admin edit dialog.
I have a "CmsObject" model, which contains several "CmsPageItem" objects (currently there will be just one fixed CmsPageItem, but that will change in the future). Rather then letting a StackedInline widget control the layout, I would like to display one additional textarea field somewhere in the change_form.html page.
What would the recommended approach to extend the ModelAdmin dialog be?
I expect I need to push a formfield somewhere, or introduce new values in the template context?
Have you looked at TabularInlines? There is an example here: http://docs.djangoproject.com/en/dev/intro/tutorial02/
I've eventually settled to implement the whole view myself. For most simple objects, implementing the inlines (either with a custom template, or without) is good enough. In this situation I require more control, so I've overwritten the entire add_view and change_view completely.
FeinCMS also does this for it's editor window.