List Info

Thread: Abstract field




Abstract field
user name
2007-05-29 04:37:19
Hi,

I would like to create a custom abstract field for a model.
This field
shouldn't affect the database, it should only be used for
passing
variables.
I need to get "request.user" when
"post_init" signal is fired. So I
was thinking about something like this:

Creating a model in view

> n = Poll(viewer=request.user,pk=poll_id)

(Viewer field should also be a required field.)

and then when post_init fires, you basically check:
> if instance.viewer is not in allowed_users: raise
Forbidden()

Any tips on how to create this kind of field or maybe any
other solutions?

Thanks, Sebastjan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Abstract field
country flaguser name
Australia
2007-05-29 04:52:34
Hi Sebastjan,

On Tue, 2007-05-29 at 11:37 +0200, Sebastjan Trepca wrote:
> Hi,
> 
> I would like to create a custom abstract field for a
model. This field
> shouldn't affect the database, it should only be used
for passing
> variables.
> I need to get "request.user" when
"post_init" signal is fired. So I
> was thinking about something like this:
> 
> Creating a model in view
> 
> > n = Poll(viewer=request.user,pk=poll_id)
> 
> (Viewer field should also be a required field.)
> 
> and then when post_init fires, you basically check:
> > if instance.viewer is not in allowed_users: raise
Forbidden()
> 
> Any tips on how to create this kind of field or maybe
any other solutions?

Have a look at
http://www.djangoproject.com/documentation/models/p
roperties/ .

You can see there that it is possible to create a property
on a model
that has a "setter", so that you can use that as a
keyword argument in
the model's initialisation. That setter can obviously do
anything you
like -- it need not be related to setting database fields.
So that might
be a solution to your problem.

The Model.__init__ function already knows to look out for
keyword
arguments that correspond to those sort of properties, so
this probably
solves your problem.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Abstract field
user name
2007-05-30 07:07:44
Great, works nicely.

I would have a follow-up question though 

What about QuerySets?

Could I do:

p = Poll.objects.get(viewer=request.user,pk=poll_id)

and get that viewer parameter passed via QuerySet to the
model?

Thanks, Sebastjan

On 5/29/07, Malcolm Tredinnick <malcolmpointy-stick.com> wrote:
>
> Hi Sebastjan,
>
> On Tue, 2007-05-29 at 11:37 +0200, Sebastjan Trepca
wrote:
> > Hi,
> >
> > I would like to create a custom abstract field for
a model. This field
> > shouldn't affect the database, it should only be
used for passing
> > variables.
> > I need to get "request.user" when
"post_init" signal is fired. So I
> > was thinking about something like this:
> >
> > Creating a model in view
> >
> > > n = Poll(viewer=request.user,pk=poll_id)
> >
> > (Viewer field should also be a required field.)
> >
> > and then when post_init fires, you basically
check:
> > > if instance.viewer is not in allowed_users:
raise Forbidden()
> >
> > Any tips on how to create this kind of field or
maybe any other solutions?
>
> Have a look at
> http://www.djangoproject.com/documentation/models/p
roperties/ .
>
> You can see there that it is possible to create a
property on a model
> that has a "setter", so that you can use that
as a keyword argument in
> the model's initialisation. That setter can obviously
do anything you
> like -- it need not be related to setting database
fields. So that might
> be a solution to your problem.
>
> The Model.__init__ function already knows to look out
for keyword
> arguments that correspond to those sort of properties,
so this probably
> solves your problem.
>
> Regards,
> Malcolm
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )