Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » getting id of logged user in admin site [RSS Feed]

#1 Oct. 27, 2005 21:21:09

Flavio C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


hi all, I've this model:

from django.core import meta
from django.models.auth import User

class Article(meta.Model):
title = meta.CharField(maxlength=255)
content = meta.TextField()
author = meta.ForeignKey(User) // problem is here, I think
categories = meta.ManyToManyField(Category)
creation_date = meta.DateTimeField('creation date', auto_now_add=True)
mod_date = meta.DateTimeField('modification date', auto_now=True)
file = meta.FileField(upload_to='/Users/go/Sites/django/files/')

class META:
admin = meta.Admin(
fields = (
(None, {'fields': ('title', 'content',
'categories')
}),
),
)

How can I save the field 'author' with the id of current user? I think
to use 'default' attribute, something like this:
author = meta.ForeignKey(User, default=???)
but how can I get current uesre's id? (user.id doesn't work)

Thanks,
Flavio Curella

Offline

#2 Oct. 28, 2005 13:44:01

Grigory F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


Hello Flavio Curella!
On Thu, 27 Oct 2005 20:20:23 -0000 you wrote:

> How can I save the field 'author' with the id of current user? I think
> to use 'default' attribute, something like this:
> author = meta.ForeignKey(User, default=???)
> but how can I get current uesre's id? (user.id doesn't work)

I have the same question :)

--
Всего наилучшего!
greg anastasia ru Григорий.

Offline

#3 Nov. 1, 2005 11:33:02

Flavio C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


Grigory Fateyev wrote:
> Hello Flavio Curella!
> On Thu, 27 Oct 2005 20:20:23 -0000 you wrote:
>
> >
> > hi all, I've this model:
> >
> > from django.core import meta
> > from django.models.auth import User
> >
> > class Article(meta.Model):
> > title = meta.CharField(maxlength=255)
> > content = meta.TextField()
> > author = meta.ForeignKey(User) // problem is here, I think
> author = meta.ForeignKey(auth.User) # May be?
>

no, :( --> NameError: name 'auth' is not defined

and with:
<code>
from django.models import auth
</code>
--> NameError: name 'User' is not defined

Other ideas?

Offline

#4 Nov. 1, 2005 13:11:54

Grigory F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


Hello Flavio Curella!
On Tue, 01 Nov 2005 03:33:01 -0800 you wrote:

>
>
> Grigory Fateyev wrote:
> > Hello Flavio Curella!
> > On Thu, 27 Oct 2005 20:20:23 -0000 you wrote:
> >
> > >
> > > hi all, I've this model:
> > >
> > > from django.core import meta
> > > from django.models.auth import User

from django.models import auth, core

> > >
> > > class Article(meta.Model):
> > > title = meta.CharField(maxlength=255)
> > > content = meta.TextField()
> > > author = meta.ForeignKey(User) // problem is here, I think
> > author = meta.ForeignKey(auth.User) # May be?
> >
>
> no, :( --> NameError: name 'auth' is not defined
>
> and with:
> <code>
> from django.models import auth
> </code>
> --> NameError: name 'User' is not defined
>
> Other ideas?
>


--
Всего наилучшего!
greg anastasia ru Григорий.

Offline

#5 Nov. 1, 2005 15:22:01

Joey C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


Try the following:

On 10/27/05, Flavio Curella <> wrote:
>
> hi all, I've this model:
>
> from django.core import meta
> from django.models.auth import User
>
> class Article(meta.Model):
> title = meta.CharField(maxlength=255)
> content = meta.TextField()
> author = meta.ForeignKey(User) // problem is here, I think
> categories = meta.ManyToManyField(Category)
> creation_date = meta.DateTimeField('creation date', auto_now_add=True)
> mod_date = meta.DateTimeField('modification date', auto_now=True)
> file = meta.FileField(upload_to='/Users/go/Sites/django/files/')
>
> class META:
> admin = meta.Admin(
> fields = (
> (None, {'fields': ('title', 'content',
> 'categories')
> }),
> ),
> )
>
> How can I save the field 'author' with the id of current user? I think
> to use 'default' attribute, something like this:
> author = meta.ForeignKey(User, default=???)
> but how can I get current uesre's id? (user.id doesn't work)
>

That code above should work fine (I'm using almost precisely that in
something I'm working on).

Try something like this to get the id:

>>> a = articles.get_list()
>>> auth = a.get_author()
>>> auth.id

cheers,
--joey

Offline

#6 Nov. 1, 2005 21:07:03

Flavio C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


your snippet works, but it returns the id of the author of an articles
already saved in the database.
What I want to do is to save the article with the id of user that is
saving the article, without the need to select an user/author from a
menu.

Offline

#7 Nov. 1, 2005 23:43:51

h.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


>What I want to do is to save the article with the id of user that is
>saving the article, without the need to select an user/author from a
>menu.

The model code doesn't have access to the request, so you are out of
luck. It's simple to do in your own applications (as you have full
control over your views there), but I don't think it's so simple with
the admin, as that's a prebuilt application and you don't have too much
ways to hook into the view functions - only into the model behaviour.

bye, Georg

Offline

#8 Nov. 2, 2005 00:45:42

Flavio C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


thanks Hugo.

I think this is a common case: I (and peraphs many people) prefer
django to other frameworks because of its admin application: half a
work is already made. I think a feature like this will be very helpful
to many people, so I will open a ticket on django's trac.

Thanks to all,
Flavio Curella.

Offline

#9 Nov. 2, 2005 12:26:02

Grigory F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


Hello Flavio Curella!
On Tue, 01 Nov 2005 16:45:08 -0800 you wrote:

>
> thanks Hugo.
>
> I think this is a common case: I (and peraphs many people) prefer
> django to other frameworks because of its admin application: half a
> work is already made. I think a feature like this will be very helpful
> to many people, so I will open a ticket on django's trac.

It is very interestingc for me. What is the number of ticket?

--
Всего наилучшего!
greg anastasia ru Григорий.

Offline

#10 Nov. 2, 2005 20:47:08

Flavio C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

getting id of logged user in admin site


hello gregory,

ticket is #714.

bye,
Flavio

Offline

  • Root
  • » Django
  • » getting id of logged user in admin site [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support