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.

2 Answers 2

1

Does this work?

for q in queryset:
    q.user.is_active = True
    q.user.save()
queryset.update(accepted=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I fixed my answer. You can try again
While this doesn't raise an error, it doesn't necessarily work: app.box.com/s/lsyotuafv878lic1mkul
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)

1 Comment

Because 'is_active' is a field of models.User (docs.djangoproject.com/en/dev/ref/contrib/auth/…), 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.