March 10th, 2009 by Rodrigo
While writing a django app, I faced the tedious task of updating the corresponding admin.py file for the models I was writing. Because I didn’t want to customize any of the admin options just yet all I had to do is insert new register entries on the file (one for model). I grew tired of this pretty soon so I wrote this elisp function to update an admin.py file easily. Hope this helps somebody
(defun rl/django-admin-all-models()
(interactive)
(let ((content " "))
(with-temp-buffer
(insert "from models import *\n")
(insert "from django.contrib import admin\n\n")
(let ((text-start (point)))
(insert-file-contents "models.py")
(keep-lines "^class.*$" text-start (point-max))
(while (re-search-forward "^class \\(\\w+\\).*" nil t)
(replace-match "admin.site.register(\\1)"))
(write-file "admin.py" nil)))))
A word of warning: this will override your current admin.py
October 24th, 2008 by Rodrigo
Sphinx is a documentation tool written for python (but could be used
on other circumstances). I’ve learning how to used and I wanted to
document a django project I’ve been working on, but when you try to
import your modules you get an exception
autodoc can't import/find module '<yourmodule>', it reported error:
"Settings cannot be imported, because environment variable
DJANGO_SETTINGS_MODULE is undefined.",please check your spelling
and sys.path
This happens because django do some enviroment settings before using
your app, so I wanted to do the same with. It was easier than I
tought, just put the following snippet in your sphinx conf.py
from MYPROJECT import settings
from django.core.management import setup_environ
setup_environ(settings)
of course, for it to work you need to modify your path, in my case was:
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/../../'))