Hi. I am new to Django using a recent svn branch on Windows with mysql
any python 2.51.  I am trying solve the following puzzle:

I am trying to override the default behavior of one of my models by
using a Manager.  My eventual goal is to only allow a certain class of
users access to this model and I would like to place the logic for
this in the model.  Here is the code:

I have a (simplified) Department class that looks like this:

class Department(models.Model):
  name = models.CharField(max_length=200)
  objects = DepartmentManager()

As you can see I am setting objects to DepartmentManager.  I am doing
this so anything that calls Department.objects.all() will use this
manager instead of the standard one.

The DepartmentManager class looks like this:
class DepartmentManager(models.Manager):

  def get_query_set(self):
    return
super(DepartmentManager,self).get_query_set().filter(name__startswith='O')

So I am setting the default query to do something else.  This works in
the console.  So if I do:
>>> from dash.models import Department
>>> a = Department.objects.all()
>>> len(a)
I get 1 - which is correct.  If that were not there I would get a lot
more than just the one entry.

So, I am doing this so I can eventually give the admin interface a
little more granular permissions.  I have to 2 questions about this
approach.  First, after this change, when I go into the admin
interface and click on the Department item, it still pulls back ALL
the departments instead of just the one as I would have expected (I am
assuming that the admin interface calls Department.objects.all() to
get the list).  This is puzzling because my approach is also mentioned
in the book here (specifically mentioning the admin interface in fact)
so I would think it should work.  Here is the quote from the book:

If you use custom Manager objects, take note that the first Manager
Django encounters (in order by which they’re defined in the model) has
a special status. Django interprets the first Manager defined in a
class as the “default” Manager. Certain operations — such as Django’s
admin site — use the default Manager to obtain lists of objects, so
it’s generally a good idea for the first Manager to be relatively
unfiltered. In the last example, the people Manager is defined first —
so it’s the default Manager.

So, does anyone know what is going on here?  Why am I still seeing all
the Department models?  I have restarted the server, etc.  Is there
any way to see what SQL the admin interface is calling as it executes?

Second, I eventually would like to have the user object available
directly to that DepartmentManager class so I can expand the
get_query_set().filter - I am not actually interested in limiting
results to 'O' :-)  I have seen a "hack" using this:

http://lukeplant.me.uk/blog.php?id=1107301634

Is this still the best way to get the request user object or is there
some other way. It was unclear in the comments.   I also tried to
explore newforms-admin a bit to see if that would help but it is quite
a lot to grasp right now because of my Django newbie status and the
current lack of documentation. Also, as far as I can tell it has not
and many not be integrated into django and I am sure if I want to use
it until it at least makes it into the main svn trunk.  Thanks very
much for your help.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to