I’ve got a nice database I’ve created in Django, and I’d like to interface with it through some python scripts outside of my website stuff. I’m curious if it’s possible to use the Django database API outside of a Django site, and if so does anyone have any info on how it can be done? Google hasn’t yielded many useful
Tag: django
django – inlineformset_factory with more than one ForeignKey
I’m trying to do a formset with the following models (boost is the primary): I want to display a form to add a Boost item, trying to do in this way : Here are my questions : 1 – When rendered, the combo box for “Creator” shows a list of “db.userInfo” (literally)! I want this to display db.userInfo.auth.username that is
Django Query That Get Most Recent Objects From Different Categories
I have two models A and B. All B objects have a foreign key to an A object. Given a set of A objects, is there anyway to use the ORM to get a set of B objects containing the most recent object created for each A object. Here’s an simplified example: So I’m looking for a query that returns
Django TemplateDoesNotExist?
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these templates, in this order: * Using loader django.template.loaders.filesystem.function: * Using loader django.template.loaders.app_directories.function: TEMPLATE_DIRS (‘/usr/lib/python2.5/site-packages/projectname/templates’,) Is it looking for /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html in this case? The weird thing is
Atomic increment of a counter in django
I’m trying to atomically increment a simple counter in Django. My code looks like this: If I understand Django correctly, this should wrap the function in a transaction and make the increment atomic. But it doesn’t work and there is a race condition in the counter update. How can this code be made thread-safe? Answer Use an F expression: either
match an alternative url – regular expression django urls
I want a Django URL with just 2 alternatives /module/in/ or /module/out/ I’m using But it matches other patterns like /module/i/, /module/n/ and /module/ou/. Any hint is appreciated :) Answer Try r’^(?P<status>in|out)/$’ You need to remove w+, which matches one or more alphanumeric characters or underscores. The regular expression suggested in bstpierre’s answer, ‘^(?P<status>w+(in|out))/$’ will match helloin, good_byeout and so
can’t see records inserted by django test case
I’m trying to provide integration to my django application from subversion through the post commit hook. I have a django test case (a subclass of unittest.TestCase) that (a) inserts a couple of records into a table, (b) spawns an svn commit, (c) svn commit runs a hook that uses my django model to look up info. I’m using an sqlite3
Get the index of an element in a queryset
I have a QuerySet, let’s call it qs, which is ordered by some attribute which is irrelevant to this problem. Then I have an object, let’s call it obj. Now I’d like to know at what index obj has in qs, as efficiently as possible. I know that I could use .index() from Python or possibly loop through qs comparing
How to dynamically compose an OR query filter in Django?
From an example you can see a multiple OR query filter: For example, this results in: However, I want to create this query filter from a list. How to do that? e.g. [1, 2, 3] -> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) Answer You could chain your queries as follows:
How to limit the maximum value of a numeric field in a Django model?
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e.g. 0.0-5.0 ? Failing that, is there any way to restrict