On Monday, March 14, 2011 07:45:23 am Igor Nemilentsev wrote:
> I am new in Django.
> I am looking for a solution for my problem.
> I want to have a base model class
> inheriting all other model classes from that class.
> In the base model class I want to check
> some permissions for users.
> So, need pass request to model class so that
> to have request.user.
> Is it possible?
> Sorry for the obscure explanation,
> I just want to know whether it is possible to go this way.

class MyModel(models.Model):
  ...
  def __init__(self, user=None, *args, **kwargs):
    super(MyModel, self).__init__(*args, **kwargs)
    self.user = user

or:

class MyModel(models.Model):
  ...
  def setUser(self, user=None):
    self.user = user


Call setUser before you call any method that requires the user. If user is 
None, you can raise an error in your model.  Though it's best to do perm 
checking in the view/templates or a form, though with the form you're going to 
be passing around request.user also, but passing it part of the __init__() is 
normal with forms.


please note this for models:

http://docs.djangoproject.com/en/dev/topics/db/models/#field-name-hiding-is-
not-permitted

Also, you might want to research the admin source to see how it handles the 
user and permission model.


Mike






-- 
A copy of the universe is not what is required of art; one of the damned
things is ample.
                -- Rebecca West

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to