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
Small piece of middleware to be able to access authentication data from everywhere in the django code.
Right ?