On 11/22/05, Simon Willison <[EMAIL PROTECTED]> wrote:
> Rails has a neat solution to this problem: http://api.rubyonrails.com/
> classes/ActiveRecord/Base.html#M000728
>
> Article.with_scope(:find => { :conditions => "blog_id = 1" }) do
> Article.find(1) # => SELECT * from articles WHERE blog_id = 1
> AND id = 1
> end
>
> In the above block, all find operations automatically have a "WHERE
> blog_id = 1" clause added to them.
>
> I'm not sure if this would be easy or even wise to duplicate in
> Django, but it's an interesting solution.
Django has where_constraints, which is undocumented and is a way of
adding arbitrary clauses to every lookup.
class Foo(meta.Model):
bar = meta.CharField(maxlength=3)
is_published = meta.BooleanField()
class META:
where_constraints = {'is_published__exact': True}
foos.get_list() returns all Foo objects with is_published set to True.
Adrian
--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org