0

I am making an admin action that attempts to take a Request_Invite's associated user and mark it as Active.

Here is my Admin.py:

def activate_user(modeladmin, reqeust, queryset):
    queryset.user.update(active=True)
    queryset.update(accepted=True)
activate_user.short_description = "Mark User as Active"

class Request_InviteAdmin(admin.ModelAdmin):
    list_display = ['user', 'accepted']
    ordering = ['user']
    actions = [activate_user]
    class Meta:
        model = Request_Invite

admin.site.register(Request_Invite, Request_InviteAdmin)

Models.py:

from django.contrib.auth.models import User
class Request_Invite(models.Model):
    user = models.OneToOneField(User)
    accepted = models.BooleanField(default=False)

    def __unicode__(self):
        return u"%s's request" % (self.user)

When trying to run the action in the admin backend, I get the error:

'QuerySet' object has no attribute 'user'

Which is referring to the line queryset.user.update(active=True)

I am having a hard time trying to figure out how to correctly query the associated user and mark it as active within the admin action function.

ApathyBear
  • 9,057
  • 14
  • 56
  • 90

2 Answers2

1

Does this work?

for q in queryset:
    q.user.is_active = True
    q.user.save()
queryset.update(accepted=True)
ZZY
  • 3,689
  • 19
  • 22
0

ZZY's answer was right and he should deserve credit , but I didn't know that the attribute of user had to be is_active instead of active. I am assuming he didn't know either based on the question itself.

The full answer would look like this:

for q in queryset:
    q.user.is_active = True
    q.user.save()
queryset.update(accepted=True)
ApathyBear
  • 9,057
  • 14
  • 56
  • 90
  • Because 'is_active' is a field of models.User (https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User), while 'active' is not. The codes look simple, so I just typed intuitive answer that came into my mind, without testing. Sorry for the mistakes – ZZY Nov 04 '14 at 02:08