djangosnippets.org: Latest snippets posted by ubernostrumhttps://djangosnippets.org/users/ubernostrum/2008-02-25T16:46:26.308897-06:00ubernostrumManager introspecting attached model
2008-02-25T16:46:26.308897-06:00ubernostrumhttps://djangosnippets.org/snippets/616/<p><a href="http://www.b-list.org/weblog/2008/feb/25/managers/#c63422">A comment on a recent blog entry of mine</a> asked about a setup where one model has foreign keys pointing at it from several others, and how to write a manager which could attach to any of those models and query seamlessly on the relation regardless of what it's named. …</p>
Freely redistributableUsing reverse() to do redirects
2007-08-24T22:57:57.718661-05:00ubernostrumhttps://djangosnippets.org/snippets/385/<p>When I initially set up my blog, I put together the archives with URL patterns like so:</p>
<ul>
<li><code>/weblog/2007/</code> goes to <code>archive_year</code></li>
<li><code>/weblog/2007/08/</code> goes to <code>archive_month</code></li>
<li><code>/weblog/2007/08/24/</code> goes to <code>archive_day</code></li>
<li><code>/weblog/2007/08/24/some-slug</code> goes to <code>object_detail</code></li>
</ul>
<p>The same patterns held for links, only the prefix was <code>/links/</code> instead of <code>/weblog/</code>.</p>
<p>For a …</p>
Freely redistributabletypygmentdown
2007-08-22T21:01:14.853214-05:00ubernostrumhttps://djangosnippets.org/snippets/381/<p>Based heavily on <a href="/snippets/119/">snippet #119</a>, this is an all-in-one function which applies Markdown and typogrify, and adds Pygments highlighting (selected from a class name or by having Pygments guess the language) to any <code><code></code> elements found in the text.</p>
<p>It also adds some niceties for picking up useful arguments …</p>
Freely redistributableEmail on new comments
2007-08-15T17:34:40.174895-05:00ubernostrumhttps://djangosnippets.org/snippets/367/<p>In response to <a href="/snippets/366/">#366</a>, this is a subclass of the <code>CommentModerator</code> class from <code>comment_utils</code> which does nothing except email the "owner" of an object whenever a new comment is posted on it; all other moderation options remain inactive.</p>
Freely redistributableQuerying on existence of a relationship
2007-08-03T00:51:12.662073-05:00ubernostrumhttps://djangosnippets.org/snippets/353/<p>When you have two models joined by a foreign key, it's common to want to retrieve a set of objects from the "target" of the foreign key based on whether there are any objects "pointing" to them. This snippet demonstrates how to do so, using the <code>extra</code> method of the …</p>
Freely redistributableSlightly better media path tag
2007-04-23T15:40:48.203348-05:00ubernostrumhttps://djangosnippets.org/snippets/198/<p>A slightly improved version of <a href="/snippets/195/">snippet #195</a> which keeps the logic but makes use of the <code>simple_tag</code> decorator to drastically simplify the code.</p>
<p>For an alternative to this sort of tag, check out the media context processor in my <a href="http://code.google.com/p/django-template-utils/">template_utils app</a>.</p>
Freely redistributableUnobtrusive comment moderation
2007-03-17T00:36:49.093099-05:00ubernostrumhttps://djangosnippets.org/snippets/112/<p><strong>Before using this snippet</strong>, please note that it's largely been superseded by <a href="http://code.google.com/p/django-comment-utils/">comment_utils</a>, which includes a more featureful and extensible version of this system, particularly with respect to additional moderation options and useful things like email notifications of comments.</p>
<p>Once upon a time I hacked the copy of …</p>
Freely redistributableGet most-commented objects
2007-03-14T22:55:23.561115-05:00ubernostrumhttps://djangosnippets.org/snippets/108/<p>This is a pretty straightforward bit of code for getting the most-commented objects of a particular model; just drop it into a custom manager for that model, and you should be good to go. Check the docstring for how to make it look at <code>Comment</code> instead of <code>FreeComment</code>.</p>
Freely redistributableGeneric markup converter
2007-03-13T11:35:37.800116-05:00ubernostrumhttps://djangosnippets.org/snippets/104/<p>I'm a big fan of Markdown, and often set up models to automatically apply it to certain fields before saving. But that's not really flexible, because if I then distribute the code someone else might want to use reStructuredText or Textile or whatever, and then they have to hack my …</p>
Freely redistributableMake tags easier with properties
2007-03-06T01:33:46.049001-06:00ubernostrumhttps://djangosnippets.org/snippets/83/<p>Jonathan Buchanan's <a href="http://code.google.com/p/django-tagging/">Django tagging</a> application is the best thing since sliced bread, and makes adding generic tagging to any model trivially easy. But you can make it just a tiny bit easier to use by setting up a property on your model for handling the tags.</p>
<p>Once you've set this …</p>
Freely redistributableUpdated version of #31
2007-02-27T14:28:27.631078-06:00ubernostrumhttps://djangosnippets.org/snippets/32/<p>This is, I think, a slightly cleaner implentation of what <a href="/snippets/31/">snippet 31</a> is trying to do; by starting off with a dictionary containing the things we want to look for, and using a list comprehension to kill anything which comes out of the form as <code>None</code>, we can avoid …</p>
Freely redistributableGetting dynamic model choices in newforms
2007-02-26T18:30:36.499186-06:00ubernostrumhttps://djangosnippets.org/snippets/26/<p>This is an excerpt from the form code used on this site; the tricky bit here is making the <code>choices</code> for the <code>language</code> field get filled in dynamically from <code>Language.objects.all()</code> on each form instantiation, so that new languages can be picked up automatically. It also adds a blank choice at …</p>
Freely redistributableUsing manager methods
2007-02-25T12:54:39.654680-06:00ubernostrumhttps://djangosnippets.org/snippets/2/<p>This is part of the user-registration code used on this site (see <a href="http://code.google.com/p/django-registration/">the django-registration project on Google Code</a> for full source code), and shows a couple of interesting tricks you can do with manager methods.</p>
<p>In this case there's a separate <code>RegistrationProfile</code> model used to store an activation key and …</p>
Freely redistributableFetching top items
2007-02-25T00:43:02.372511-06:00ubernostrumhttps://djangosnippets.org/snippets/1/<p>This is a method from the custom manager for the Snippet model used on this site; the basic idea is to be able to ask for the top <code>n</code> "foo", where "foo" is something related to Snippet. For example, you can use <code>top_items('tag')</code> to get the top Tags ordered by …</p>
Freely redistributable