Cron Job that access django Models - django

What i have done?
I have written a small application in django with few models with sqlite3 as backend. Now i want to write a python code that clears the database elements based on certain condition.
Question:
How can i achieve the above requirement?

I think the cleanest way to do this is to write your own django-admin command.
You can then run the command using manage.py:
python manage.py your_command
Having a command that can be run in shell, you can easily put in into your crontab. Optionally django-admin commands can receive command line arguments, if needed.

Related

Wagtail "schedule_published_pages" Management Command

I was wondering why my scheduled posts were not working automatically in Wagtail, but I see in the documentation that a management command is needed to make this happen. I am unfamiliar with writing custom management commands, and I am wondering how to make the python manage.py publish_scheduled_pages command fire off automatically every hour?
Where would this code go in the document tree? Is there code that I just need to drop in and it runs from there? Or is something required on the server to run these commands on a schedule?
Any help would be appreciated. I couldn't find any existing code anywhere for this functionality in Wagtail and I'm wondering why the button is in the admin to schedule a post, but the functionality is not already built in?
You are probably familiar with management commands since python manage.py runserver and makemigrations and migrate are management commands.
You can see all available commands with python manage.py -h
publish_scheduled_pages should be called periodically. Form the Wagtail docs:
This command publishes, updates or unpublishes pages that have had these actions scheduled by an editor. We recommend running this command once an hour.
Periodically executing a command can be done in various ways. Via crontab is probably the most common. To edit the crontab:
$ crontab -e
Add (for every fist minute of the hour):
0 * * * * python /path/to/your/manage.py publish_scheduled_pages --settings=your.settings

Validating django models on runserver command

I want to create some custom validation on models when I do python manage.py runserver command.
Can I do this somehow?
Well, the problem in fact is that you want to run some custom code when your server starts up.
Two possibilities:
Use wsgi.py as suggested by this post. This seems to be the more elegant solution.
Put all your code inside some __init__.py file. Be warn that this code will be executed any time you import that package (for example, when you run a django command)

In Django's shell, how can I load ipython magic functions automatically?

I want to make the following ipython commands permanent when using django shell:
%load_ext autoreload
%autoreload 2
Unfortunately, Django doesn't seem to use my global ipython config, so putting them in my default_profile doesn't seem to work. Is there any way to have these executed automatically when running django shell?
You can use the django extentions package, which contains a shell_plus command. This command autoloads the models, but you also can use the --notebook attribute. There you can add the autoload parameter: --ext django_extensions.management.notebook_extension.
See here for more info.

Executing shell commands from a Django app/project

I want to be able to execute some shell commands like mkvirtualenv and createdb and such from a Django app, these processes will likely go into a Django celery task later. I would think using system() would be insecure to execute shell commands from Django/Apache, so my question is what is the best way to execute shell commands from a django app?
To start a shell process in the background you can use the subprocess module.

Is there a way to add custom django-admin.py commands that work outside of projects?

I'm trying to write a custom command that works outside of Django projects. I was thinking I could follow the coding patterns of Django's own such commands (e.g., startproject), include my command in an app and install it.
Alas, it seems django cannot see this command, as perhaps it doesn't scan site-packages for custom commands.
Is there a way to make this work or am I sadly correct?
UPDATE: I should note that the goal I was trying to accomplish (writing a command that starts projects based on custom templates) is supported in the coming 1.4 release of Django: https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-startproject (see the --template option).
Based on this code from django.core.management, it does appear that django only searches for project-less commands in its own packages, and will then only find command by scanning INSTALLED_APPS, which means a project is required.
You can use a custom manage.py.
You do need a project. A project is, although, nothing more than a python package with a settings.py (and maybe a urls.py file)
So you could just create a project, with whatever commands you want, and in your setup script include a binary script that is nothing more than a manage.py in disguise.
I use it to have a manage.py in the bin path of a virtualenv, but you can call it something else and have that "django" project installed in your system python.
I don't quite understand from your post, for what purpose do You want to write such command using Django's manage.py. But suppose you want (as I was) to run some script, that works with Django models, for example. You cannot run such script without setting Django environment.
I do the following:
put my code in script.py
manage.py shell
execfile('script.py')
Maybe, this helps.