Django Admin, Call function on save - python

I have a app in my Django site that handles blog post. When a blog post is published I want to schedule a Newsletter on a third party application that informs the subscribers of the new post.
So I want to add a custom function to be called when the blog post is saved where I can write this API call to the newsletter service.
How to do this? Tried looking through the documentations and all I can find is Admin Actions which doesn't seem to be what I'm looking for.

There are a number of approaches you could use.
Override the model save method is simple, but will be called every time the model is saved.
https://docs.djangoproject.com/en/dev/ref/models/instances/#saving-objects
If it is specific to the admin site, in your ModelAdmin use the model_save() method. (I like this approach personally, as it won't interfere with your model).
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
You could also use a post save signal, but save methods seem to be preferred (depending on what you are doing) Django: When to customize save vs using post-save signal

You should definitely go for https://docs.djangoproject.com/en/1.8/ref/signals/#django.db.models.signals.post_save, which provides the complete functionality that you are looking for here.

Related

How to add custom view and actions to django admin panel?

I'm new at Python and Django development. I'm trying to make a simple blog.
Right now what i am trying to do is create a admin function to show comments that not approved by admin yet. I like to show that comments and offer accept or reject choices to admin.
The problem is, i don't know the steps. I made some searches online but couldn't find what we are looking for. What are the steps to do this? What i need to learn to do this?
How do i add add custom list, buttons and functionality? I don't event know where to write code for my custom admin functionality.
Ps: I'm not looking for someone to write code for me. I'm just looking for guidelines.
I recommend using django-admin-plus (https://github.com/jsocol/django-adminplus) which does exactly what you want:
AdminPlus aims to be the smallest possible extension to the excellent Django admin component that lets you add admin views that are not tied to models.
All AdminPlus does is allow you to add simple custom views (well, they can be as complex as you like!) without mucking about with hijacking URLs, and providing links to them right in the admin index.

How do I display a confirmation message after a custom admin action with an intermediate page (in Django)?

The built-in actions that come with the Django admin generally display a helpful message after they execute at the top, e.g. saying that a new object was added or what have you.
The docs show how to do it with simple actions that can be represented as methods of the custom ModelAdmin. However, with custom actions that need intermediate pages (covered further down on that same page), I am encouraged to pass the user onto another view. That's great, but it means that I no longer have access to the custom ModelAdmin instance in order to call its message_user() method... Or at least I'm not sure how to get it.
Can you tell me how to get a hold of the current ModelAdmin instance or, if there's a better way, how else to display one of those helpful little messages when I'm done in the other view?
To mimic the ModelAdmin.message_user method you only need to do the following:
from django.contrib import messages
messages.info(request, message)
Adding a message is documented here https://docs.djangoproject.com/en/dev/ref/contrib/messages/#adding-a-message and the way ModelAdmin uses it can be seen here: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L691
Construct a LogEntry and write a custom templatetag to render messages on your intermediat page, for instance:
LogEntry.objects.log_action(
user_id=request.user.id,
content_type_id=ContentType.objects.get_for_model(yourmodel).pk,
object_id=case.id,
object_repr=force_unicode(yourmodel),
action_flag=ADDITION if created else CHANGE)
read more: Django docs (Message Framework)

How do I get admin.StackedInline or admin.TabularInline like feel in my own views?

Consider the simple Poll example in the Django Tutorial example in Djnagoproject web site. Once you use admin.StackedInline or TabularInline, you get a nice interface which allows you create a neat Poll application.
If I need to provide same look, feel and functionality in my own custom views, what should I do. For eg: I want to let users create polls, but they don't have access to the admin section, but we can provide a view like "/create/polls/".
Any ideas, code, pointers will be helpful
Inline formsets.

Where can I get a template website?

I want to benchmark the performance of a template website on a modified kernel. I want to use a website template that has 2-3 tiers (frontend, database etc), logic to create users and some logic to store/modify data for each user.
A quick search did not reveal any useful results as of yet.
I've little experience in web development and was hoping that stackoverflow can point me to something useful.
I would suggest taking a look at the Django framework:
http://docs.djangoproject.com/en/1.3/intro/
http://www.djangobook.com/en/2.0/
Django operates using a three tiered (Model, Template, View) design. The Model is the database access layer and will enable you to validate and store information about your users. The Template is the 'presentation layer' that will both determine the layout of your page through html, but has access to your view and its variables. The View is the portion that will contain all of the logic for the page - in a way it works as a median between your model and your template. The url your user visits will determine which view function you load.
If you are interested in the admin capabilities of the framework, take a look at:
http://www.djangobook.com/en/2.0/chapter06/
You could simply download and run one of the sample django applications like:
http://code.google.com/p/django-voting/
or
https://github.com/scrum8/django-job-board/
Or you could just create a clean django project and turn on the admin console.

Calling an external script from Django admin interface?

I have a Django admin interface that is used almost solely as a gui form for making changes to a single postgresql table. There's also a Python script that's currently run manually from the command line whenever a change is made to the database, & I'd like to hook that up so it runs whenever someone hits "save" after making a change to a row of the table via the admin interface. If this was an entry in views.py, it looks like I'd import the script as a module and run its main function from the view (ie, Can Django use "external" python scripts linked to other libraries (NumPy, RPy2...)). I'm not sure, however, how to do this in the admin interface.
How is admin.py similar/different to a regular entry in views.py?
Where do I put the import/call to the external script - somewhere in the model, somewhere in admin.py?
I'm familiar with Python, but am fairly new to (& somewhat mystified by) "web stuff" (ie, frameworks like Django), & I'm not even sure if I'm asking this question very clearly, because I'm still a little fuzzy on the view/model concept ...
Edit: Turns out I had, in fact, found the solution by reading the documentation/tutorial, but assumed there was a difference with admin stuff. As Keith mentioned in the comments, I'm now running into permissions issues, but I guess that's a separate problem. So thanks, & maybe I'll stop second guessing myself ...
Generally, things you want to happen at 'save' time are either
Part of the model.
If so, you override the model's save method: http://docs.djangoproject.com/en/1.3/ref/models/instances/#saving-objects
You can do anything in that save method.
Part of the view function.
If so, you either extend the admin interface (not so easy), or you write your own.
One thing you might consider is defining the save_model method in your ModelAdmin. This will get executed when someone saves from the admin (but not when someone does a save outside of the admin). This approach might depend on what your requirements are, but should give you the necessary hook when doing the save from the admin.
In admin.py
class MyModelAdmin(admin.ModelAdmin):
model = models.MyModel
def save_model(self, request, obj, form, change):
# you can put custom code in here
obj.save()

Categories