Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnonymousUser doesn't have attribute '_meta'?! #122

Closed
ckcollab opened this issue Oct 31, 2012 · 10 comments
Closed

AnonymousUser doesn't have attribute '_meta'?! #122

ckcollab opened this issue Oct 31, 2012 · 10 comments

Comments

@ckcollab
Copy link

AttributeError: 'AnonymousUser' object has no attribute '_meta'

It's getting hit at this line

class DisplayActivityActorUrl(Node):
    def __init__(self, actor):
        self.actor = Variable(actor)

    def render(self, context):
        actor_instance = self.actor.resolve(context)

        # get_for_model has error here
        content_type = ContentType.objects.get_for_model(actor_instance).pk
        return reverse('actstream_actor', kwargs={
            'content_type_id': content_type, 'object_id': actor_instance.pk})

Traceback (most recent call last):

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "./LoLKarma/views.py", line 415, in player_detail
    'activities': activities if activities else None,

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 44, in render
    return HttpResponse(loader.render_to_string(*args, **kwargs),

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/loader.py", line 176, in render_to_string
    return t.render(context_instance)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 281, in render
    return nodelist.render(context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)

  File "/home/gggamers/gggamers/apps/actstream/templatetags/activity_tags.py", line 39, in render
    content_type = ContentType.objects.get_for_model(actor_instance).pk

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 32, in get_for_model
    opts = self._get_opts(model)

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 20, in _get_opts
    return model._meta.concrete_model._meta

  File "/home/gggamers/gggamers/local/lib/python2.7/site-packages/django/utils/functional.py", line 185, in inner
    return func(self._wrapped, *args)

AttributeError: 'AnonymousUser' object has no attribute '_meta'

I edited managers.py and am using my own stream

    @stream
    def model_actions_with_id(self, model, id, **kwargs):
        """
        Stream of most recent actions by any particular model
        """
        ctype = ContentType.objects.get_for_model(model)
        return self.public(
            (Q(target_content_type=ctype, target_object_id = id) |
             Q(action_object_content_type=ctype, action_object_object_id = id) |
             Q(actor_content_type=ctype, action_object_object_id = id)),
            **kwargs
        )

I use that to select all activities a user was rated in, or all activies in which the user was rated. This is the user displayed on the page, NOT the user viewing the page! I can't find any reference to request.user, so I have no idea why it matters that the user is Anonymous! This works perfectly with logged in users.

I am also doing caching for non logged in users which might be doing something... but I have no idea how!!

@ckcollab
Copy link
Author

ckcollab commented Oct 31, 2012

ERRR!!!

This was my fault, sorry to bother... the error in my email had very few details, when I got to the office today it was obvious what my mistake was in the traceback. Sorry to bother.

AnonymousUser isn't a real user, obviously!

@zzantares
Copy link

zzantares commented Jun 17, 2016

Hello @ckcollab I'm having the exact same issue, however is not clear to me what you did to fix it, can you elaborate on that? I'm not finding any answers to this 😕

I know it was a long time, but I have no clue, what I'm just doing is log in a user after registration.

Thanks!

@ckcollab
Copy link
Author

Well, is it an AnonymousUser? Post some code and I can help maybe, but I think in my last message I was talking about how it was obvious I was dealing with an AnonymousUser and shouldn't be trying to get activity streams for them?

@zzantares
Copy link

Thanks for your offer to help, my problem was solved since I noticed I haven't use set_password to set the password of a recently registered user, thus when trying to login the user I was getting a AnonymousUser back.

For others reading realise what the error says "You're dealing with an AnonymousUser", once I thought on this it was clear my login wasn't working at all.

@agrawalharish1997
Copy link

Hello @ckcollab I'm having the exact same issue, however is not clear to me what you did to fix it, can you elaborate on that? I'm not finding any answers to this. .It come error as ('AnonymousUser' object has no attribute '_meta')

@ckcollab
Copy link
Author

ckcollab commented Jan 18, 2020

@agrawalharish1997 actually, the way activity stream is built a user is required. You can't use an AnonymousUser. You can see the work one of our engineers (thanks Chris! @ReproKillbot) did towards adding this ability:
#442

The pull request, for good reason, was denied. You can use this fork though and make actions with no user attached!

@agrawalharish1997
Copy link

agrawalharish1997 commented Jan 18, 2020 via email

@ckcollab
Copy link
Author

..I'm not sure about that..

@AmiteyeEmmanuel
Copy link

Hello please I am having the same issue please can someone explain how to actually set password for this error

@ckcollab
Copy link
Author

@AmiteyeEmmanuel you can do something like this..

user = User.objects.get(email="someguy@gmail.com")
user.set_password("this is a password that gets hashed and saved to database")
user.save()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants