user._meta has no attribute 'pk' causes error while storing session #48

Closed
omerra opened this Issue Feb 14, 2016 · 7 comments

Projects

None yet

2 participants

@omerra
omerra commented Feb 14, 2016

I think this issue was already present in the old project, and was closed when the split occured.
It seemed that with the latest github build of this project, and django 1.9.2 this still isn't fixed.
I get the Exception
AttributeError: 'MetaDict' object has no attribute 'pk'
On the login function in: django.contrib.auth.init.py"

The error is on request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)

the _meta dict doesn't have the .pk attribute.
Is there any quick fix I can implement my self?

@omerra
omerra commented Feb 14, 2016

I looked over the past Issues, it seems this was solved, and somehow it was broken again. I tried re adding the 2 lines that were removed from the models.py file:
User._meta.pk = User._fields["id"]
User._meta.pk.value_to_string = lambda obj: smart_text(obj.pk)

However it then failed on setting this attribute, I guess the _meta.pk property is not editable there.
To solve it for now I added these same 2 lines to our own User models.py file, so that now it has the _meta.pk attribute. This seems to work. However I think there should be a solution that will make this work straight from mongoengine

@last-partizan
Contributor

Hello. I've removed that hack becouse i was added value_to_string method to PkWrapper.

Just checked login on example project, and it works.

@omerra
omerra commented Feb 14, 2016

Hi,first thanks for the help.
It doesn't work on our project, our settings are as shown in the example.

AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'models.users.User'

We use a custom User model that we created using mongoengine. Not the default model from the auth.
The problem is that the user model that authenticate returns is our own User model, and not mongo_auth.MongoUser.
even if we do MongoUser.objects.get(email="user_email") we get our User model. And so it doesn't containt the PkWrapper and doesn't have the _meta.pk attribute.
Like I said, If we add the _meta.pk attribute to our own model it solves it.
It should be possible to work with a custom User Mongoengine model, and still authenticate in my opinion. It also used to work this way before the split.
Do you have any ideas how to solve this?

@last-partizan
Contributor

Probably your custom User model uses mongoengine.Document instead of django_mongoengine.Document, so wrappers does not applied

@omerra
omerra commented Feb 14, 2016

Yes you are right, our custom user model inherits straight from mongoengine.Document.
So the solution should be to inherit from django_mongoengine.Document ?
I see that that inherits from mongoengine.Document it self. So it should be transparent and work, if we inherit from django_mongoengine.Document ?

@last-partizan
Contributor

Yes, django_mongoengine.Document just add that missing properties required by django stuff

@omerra
omerra commented Feb 14, 2016

Great, thanks a lot for the help, and for maintaining this library, will try with this change and test it out.
Maybe add to the docs somewhere, that if you use a custom model, you should inherit from django_mongoengine.Document (since before the split, there was no difference between the 2).

This also works with our own custom authentication backends by the way. So it should basically work with any authentication backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment