Tuesday, August 20, 2013

Re: Using django.contrib.auth...

Thanks very much Laurent, I will have a go with that this morning.
Regards, N/

On Monday, 19 August 2013 16:25:00 UTC+1, Laurent Meunier wrote:
On 19/08/2013 17:05, Nigel Legg wrote:
> In the app I am building for data analysis, I want all logged in users
> to be able to access all parts of the system, but only to have access to
> the datafiles they hae uploaded themselves.  Can I use
> django.contrib.auth to filter the full list of datafiles according to
> the user object?
> If so, how do I go about this? I'm thinking of attaching a user_id field
> to the file when it is uploaded, and then filtering against that wen a
> logged in user goes to the list of files, but I'm not sure how I would
> implement this.
> Any help greatly appreciated.

Hi,

Your best option is to add a foreign key to your user model (usually
django.contrib.auth.models.User) in your model that handles your datafiles.

from django.contrib.auth.models import User

class Datafile(models.Model):
     user = models.ForeignKey(User)
     ...


When a user upload a datafile, don't forget to set the user field:

mydatafile.user = request.user


And then you can filter datafiles:

Datafile.objects.filter(user=request.user)


--
Laurent Meunier <lau...@deltalima.net>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate