Re: Accessing request.user in a ModelAdmin function
Hi Alireza, Yes, that did the trick! Thank you, Paulo On Thursday, June 14, 2012 2:26:46 PM UTC+1, Alireza Savand wrote: > > > > On Thursday, June 14, 2012 3:04:54 PM UTC+4, Paulo Almeida wrote: >> >> Hi, >> >> I would like to access the request.user in a ModelAdmin function, so I >> can filter the results of a query based on the logged in user. I have these >> models: >> >> class Speaker(models.Model): >> name = models.CharField(max_length=50) >> endorsements = models.ManyToManyField(User, >> >> through="Endorsement") >> >> class Endorsement(models.Model): >> speaker = models.ForeignKey('Speaker') >> user = models.ForeignKey(User) >> endorsed = models.NullBooleanField() >> >> class Meta: >> unique_together = ("speaker", "user") >> >> User comes from django.contrib.auth.models. In the ModelAdmin, I would >> like to have this: >> >> class SpeakerAdmin(admin.ModelAdmin): >> def is_endorsed(self, obj): >> >> endorsement = Endorsement.objects.get(speaker=obj, >> >> user=request.user) >> return endorsement.endorsed >> >> So then I could just add "is_endorsed" to the list_display variable to >> have the endorsement status for that user in the Speaker list, in the Admin >> site. Of course, this doesn't work because request isn't available in the >> is_endorsed function. I googled around and saw solutions for similar >> problems involving overrides of save_model, queryset or >> formfield_for_manytomany, but I couldn't adapt them to list_display, >> because I'm creating a function in the ModelAdmin, where I only know how to >> pass self and obj. Suggestions? >> >> Thanks, >> Paulo Almeida >> > > I think you looking for this app > http://pypi.python.org/pypi/django-cuser/ > > Small piece of middleware to be able to access authentication data from > everywhere in the django code. > > > Right ? > > On Thursday, June 14, 2012 2:26:46 PM UTC+1, Alireza Savand wrote: > > > > On Thursday, June 14, 2012 3:04:54 PM UTC+4, Paulo Almeida wrote: >> >> Hi, >> >> I would like to access the request.user in a ModelAdmin function, so I >> can filter the results of a query based on the logged in user. I have these >> models: >> >> class Speaker(models.Model): >> name = models.CharField(max_length=50) >> endorsements = models.ManyToManyField(User, >> >> through="Endorsement") >> >> class Endorsement(models.Model): >> speaker = models.ForeignKey('Speaker') >> user = models.ForeignKey(User) >> endorsed = models.NullBooleanField() >> >> class Meta: >> unique_together = ("speaker", "user") >> >> User comes from django.contrib.auth.models. In the ModelAdmin, I would >> like to have this: >> >> class SpeakerAdmin(admin.ModelAdmin): >> def is_endorsed(self, obj): >> >> endorsement = Endorsement.objects.get(speaker=obj, >> >> user=request.user) >> return endorsement.endorsed >> >> So then I could just add "is_endorsed" to the list_display variable to >> have the endorsement status for that user in the Speaker list, in the Admin >> site. Of course, this doesn't work because request isn't available in the >> is_endorsed function. I googled around and saw solutions for similar >> problems involving overrides of save_model, queryset or >> formfield_for_manytomany, but I couldn't adapt them to list_display, >> because I'm creating a function in the ModelAdmin, where I only know how to >> pass self and obj. Suggestions? >> >> Thanks, >> Paulo Almeida >> > > I think you looking for this app > http://pypi.python.org/pypi/django-cuser/ > > Small piece of middleware to be able to access authentication data from > everywhere in the django code. > > > Right ? > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/yJUcQhE-278J. 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.
Re: Accessing request.user in a ModelAdmin function
And if it's just for logged-in user, you can do like https://gist.github.com/2930287 at the ModelAdmin -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/mxxG2VwgMeMJ. 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.
Re: Accessing request.user in a ModelAdmin function
On Thursday, June 14, 2012 3:04:54 PM UTC+4, Paulo Almeida wrote: > > Hi, > > I would like to access the request.user in a ModelAdmin function, so I can > filter the results of a query based on the logged in user. I have these > models: > > class Speaker(models.Model): > name = models.CharField(max_length=50) > endorsements = models.ManyToManyField(User, > > through="Endorsement") > > class Endorsement(models.Model): > speaker = models.ForeignKey('Speaker') > user = models.ForeignKey(User) > endorsed = models.NullBooleanField() > > class Meta: > unique_together = ("speaker", "user") > > User comes from django.contrib.auth.models. In the ModelAdmin, I would > like to have this: > > class SpeakerAdmin(admin.ModelAdmin): > def is_endorsed(self, obj): > > endorsement = Endorsement.objects.get(speaker=obj, > > user=request.user) > return endorsement.endorsed > > So then I could just add "is_endorsed" to the list_display variable to > have the endorsement status for that user in the Speaker list, in the Admin > site. Of course, this doesn't work because request isn't available in the > is_endorsed function. I googled around and saw solutions for similar > problems involving overrides of save_model, queryset or > formfield_for_manytomany, but I couldn't adapt them to list_display, > because I'm creating a function in the ModelAdmin, where I only know how to > pass self and obj. Suggestions? > > Thanks, > Paulo Almeida > I think you looking for this app http://pypi.python.org/pypi/django-cuser/ Small piece of middleware to be able to access authentication data from everywhere in the django code. Right ? -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/7zNoE-as3asJ. 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.
Re: Accessing request.user in a ModelAdmin function
Hi again, >From what I read in the 1.4 docs, the SimpleListFilter example is for list_filter and not list_display. To clarify what I wrote before, if a user endorses Speaker A and opposes Speaker B, I want the Speaker list in the admin site to show: |Name | Is endorsed | |A | True| |B | False | Having a filter on "Is Endorsed" might be a nice addition, but the main goal is to have that table. Would there be a way to get the result of a SimpleListFilter into a callable that can be used in list_display? I didn't see that in the docs, and it's not obvious to me how to do it. Thanks, Paulo On Thursday, June 14, 2012 1:09:58 PM UTC+1, Paulo Almeida wrote: > > Hi Melvyn, > > I've only been reading the Django 1.2 docs, because it's what's > immediately available in my Linux distribution, but that would be an > excellent reason to upgrade, if I can get it to work. > > Thanks, > Paulo > > On Thursday, June 14, 2012 12:33:34 PM UTC+1, Melvyn Sopacua wrote: >> >> On 14-6-2012 13:04, Paulo Almeida wrote: >> >> > So then I could just add "is_endorsed" to the list_display variable to >> have >> > the endorsement status for that user in the Speaker list, in the Admin >> > site. Of course, this doesn't work because request isn't available in >> the >> > is_endorsed function. I googled around and saw solutions for similar >> > problems involving overrides of save_model, queryset or >> > formfield_for_manytomany, but I couldn't adapt them to list_display, >> > because I'm creating a function in the ModelAdmin, where I only know >> how to >> > pass self and obj. Suggestions? >> >> In 1.4 you have ModelAdmin.list_filter and can subclass >> SimpleListFilter. These get passed the request and the documentation >> provides a full example of what you're trying to do. >> >> -- >> Melvyn Sopacua >> > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/xXv1KzZUaYUJ. 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.
Re: Accessing request.user in a ModelAdmin function
Hi Melvyn, I've only been reading the Django 1.2 docs, because it's what's immediately available in my Linux distribution, but that would be an excellent reason to upgrade, if I can get it to work. Thanks, Paulo On Thursday, June 14, 2012 12:33:34 PM UTC+1, Melvyn Sopacua wrote: > > On 14-6-2012 13:04, Paulo Almeida wrote: > > > So then I could just add "is_endorsed" to the list_display variable to > have > > the endorsement status for that user in the Speaker list, in the Admin > > site. Of course, this doesn't work because request isn't available in > the > > is_endorsed function. I googled around and saw solutions for similar > > problems involving overrides of save_model, queryset or > > formfield_for_manytomany, but I couldn't adapt them to list_display, > > because I'm creating a function in the ModelAdmin, where I only know how > to > > pass self and obj. Suggestions? > > In 1.4 you have ModelAdmin.list_filter and can subclass > SimpleListFilter. These get passed the request and the documentation > provides a full example of what you're trying to do. > > -- > Melvyn Sopacua > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/VGhuIwcMj9sJ. 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.
Re: Accessing request.user in a ModelAdmin function
On 14-6-2012 13:04, Paulo Almeida wrote: > So then I could just add "is_endorsed" to the list_display variable to have > the endorsement status for that user in the Speaker list, in the Admin > site. Of course, this doesn't work because request isn't available in the > is_endorsed function. I googled around and saw solutions for similar > problems involving overrides of save_model, queryset or > formfield_for_manytomany, but I couldn't adapt them to list_display, > because I'm creating a function in the ModelAdmin, where I only know how to > pass self and obj. Suggestions? In 1.4 you have ModelAdmin.list_filter and can subclass SimpleListFilter. These get passed the request and the documentation provides a full example of what you're trying to do. -- Melvyn Sopacua -- 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.
Accessing request.user in a ModelAdmin function
Hi, I would like to access the request.user in a ModelAdmin function, so I can filter the results of a query based on the logged in user. I have these models: class Speaker(models.Model): name = models.CharField(max_length=50) endorsements = models.ManyToManyField(User, through="Endorsement") class Endorsement(models.Model): speaker = models.ForeignKey('Speaker') user = models.ForeignKey(User) endorsed = models.NullBooleanField() class Meta: unique_together = ("speaker", "user") User comes from django.contrib.auth.models. In the ModelAdmin, I would like to have this: class SpeakerAdmin(admin.ModelAdmin): def is_endorsed(self, obj): endorsement = Endorsement.objects.get(speaker=obj, user=request.user) return endorsement.endorsed So then I could just add "is_endorsed" to the list_display variable to have the endorsement status for that user in the Speaker list, in the Admin site. Of course, this doesn't work because request isn't available in the is_endorsed function. I googled around and saw solutions for similar problems involving overrides of save_model, queryset or formfield_for_manytomany, but I couldn't adapt them to list_display, because I'm creating a function in the ModelAdmin, where I only know how to pass self and obj. Suggestions? Thanks, Paulo Almeida -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Nve_nfYdC5cJ. 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.