Custom user model in social_auth+mongoengine+django

275 views
Skip to first unread message

Shrikrishna Holla

unread,
Apr 15, 2013, 2:32:40 AM4/15/13
to django-so...@googlegroups.com
I would be grateful if you guys could clarify have a small doubt that I have regarding custom user models

When I create a custom user class, can I subclass UserSocialAuth? Or do I have to inherit from orm's user model? Or is it better to go native and inherit from the base model and define everything (username etc) manually?

I'm asking because I only want fields in addition to the ones UserSocialAuth already provides

If it's helpful, I'm using Django 1.4.4 with mongoengine 0.7.9 sessions and models and social_auth v0.7.22

--

Shrikrishna Holla |  KDE Dev |  ISE, PESIT | Google Student Ambassador

Matías Aguirre

unread,
Apr 15, 2013, 1:54:25 PM4/15/13
to django-social-auth
UserSocialAuth is not a user model, it's used to keep track of accounts
association.

Mongoengine has its own User model[1] and I guess you can inherit from it.

[1]: https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/django/auth.py#L39

Excerpts from Shrikrishna Holla's message of 2013-04-15 03:32:40 -0300:
> I would be grateful if you guys could clarify have a small doubt that I
> have regarding custom user models
>
> When I create a custom user class, can I subclass UserSocialAuth? Or do I *
> have* to inherit from orm's user model? Or is it better to go native and
> inherit from the base model and define everything (username etc) manually?
>
> I'm asking because I only want fields in addition to the
> ones UserSocialAuth already provides
>
> If it's helpful, I'm using Django 1.4.4 with mongoengine 0.7.9 sessions and
> models and social_auth v0.7.22
>
--
Matías Aguirre (matias...@gmail.com)

Shrikrishna Holla

unread,
Apr 16, 2013, 7:48:15 AM4/16/13
to django-so...@googlegroups.com
Thank you Matías, I did as you suggested. But I'm finding that the profiles are still being stored in mongoengine.django.auth.User and not my CustomUser, though I have set SOCIAL_AUTH_USER_MODEL appropriately. Why is this so?

If that helps, my settings is http://paste.kde.org/725552/ . This is a huge problem because, when I try to save extra details to my custom user through the pipeline, I find that the user is actually a mongoengine.django.auth.User and hence try to save all details again in CustomUser and this leads to NotUniqueError because the subclass's (CustomUser) object clashes with the parent's (mongoengine.django.auth.User) object and it finds an attempt at creation of duplicates

Matías Aguirre

unread,
Apr 16, 2013, 11:56:28 AM4/16/13
to django-social-auth
Hi,

I'm not experienced with mongoengine, probably somebody else can answer your
question in a better way. By inspecting your settings this didn't make sense to
me, why is there a 'app.models.CustomUser' in your AUTHENTICATION_BACKENDS
setting? Also, showing your custom user model will help.

Thanks,
Matías

Excerpts from Shrikrishna Holla's message of 2013-04-16 08:48:15 -0300:
> Thank you Matías, I did as you suggested. But I'm finding that the profiles
> are still being stored in mongoengine.django.auth.User and not my
> CustomUser, though I have set SOCIAL_AUTH_USER_MODEL appropriately. Why is
> this so?
>
> If that helps, my settings is http://paste.kde.org/725552/ . This is a huge
> problem because, when I try to save extra details to my custom user through
> the pipeline, I find that the user is actually a mongoengine.django.auth.User
> and hence try to save all details again in CustomUser and this leads to NotUniqueError
> because the subclass's (CustomUser) object clashes with the parent's (
> mongoengine.django.auth.User) object and it finds an attempt at creation of
> duplicates
>
> On Monday, 15 April 2013 23:24:25 UTC+5:30, Matías Aguirre wrote:
> >
> > UserSocialAuth is not a user model, it's used to keep track of accounts
> > association.
> >
> > Mongoengine has its own User model[1] and I guess you can inherit from it.
> >
> > [1]:
> > https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/django/auth.py#L39
> >
> > Excerpts from Shrikrishna Holla's message of 2013-04-15 03:32:40 -0300:
> > > I would be grateful if you guys could clarify have a small doubt that I
> > > have regarding custom user models
> > >
> > > When I create a custom user class, can I subclass UserSocialAuth? Or do
> > I *
> > > have* to inherit from orm's user model? Or is it better to go native and
> > > inherit from the base model and define everything (username etc)
> > manually?
> > >
> > > I'm asking because I only want fields in addition to the
> > > ones UserSocialAuth already provides
> > >
> > > If it's helpful, I'm using Django 1.4.4 with mongoengine 0.7.9 sessions
> > and
> > > models and social_auth v0.7.22
> > >
> > --
> > Matías Aguirre (matias...@gmail.com <javascript:>)
> >
>
--
Matías Aguirre (matias...@gmail.com)

Shrikrishna Holla

unread,
Apr 16, 2013, 1:12:29 PM4/16/13
to django-so...@googlegroups.com
By inspecting your settings this didn't make sense to
me, why is there a  'app.models.CustomUser' in your AUTHENTICATION_BACKENDS
setting?

That was just something that I did to try and get it to work. Have already removed it
 
Also, showing your custom user model will help.


http://paste.kde.org/725750/ . There are traces of dirty hacks there too. It was in update_user that I figured out that the CustomUser class wasnt being used at all. Is there something I'm doing wrong there? 

Regards,
Shrikrishna Holla 

Matías Aguirre

unread,
Apr 16, 2013, 3:06:54 PM4/16/13
to django-social-auth
A couple of comments, your manager is inheriting from auth.User which doesn't
make sense actually. Second, DSA is not supporting custom users when using
mongoengine backend at the moment and I'm working to get that fixed, will post
shortly about this change which should solve your problem.

Thanks,
Matías

Excerpts from Shrikrishna Holla's message of 2013-04-16 14:12:29 -0300:
--
Matías Aguirre (matias...@gmail.com)

Matías Aguirre

unread,
Apr 16, 2013, 3:19:28 PM4/16/13
to django-social-auth
I've pushed support for custom user models when using mongoengine backend,
here's the changeset [1]. Shrikrishna, could you give it a try?

[1]: https://github.com/omab/django-social-auth/commit/7ee4618830e43623f70e993f0747b637482c5964

Thanks,
Matías

Excerpts from Matías Aguirre's message of 2013-04-16 16:06:54 -0300:

Shrikrishna Holla

unread,
Apr 16, 2013, 8:47:32 PM4/16/13
to django-so...@googlegroups.com
I used CustomUserManager inherited from auth.User because http://django-social-auth.readthedocs.org/en/latest/configuration.html#custom-user-model said that there must be a manager for it to work, which should contain create_user. That method was contained already in auth.User, so I wrote that as a dummy piece pointing back to CustomUser's create_user which I thought, might be needed for social_auth

And sure! I will try it!


--
You received this message because you are subscribed to a topic in the Google Groups "Django Social Auth" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-social-auth/ybqafJCplVI/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-social-a...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Matías Aguirre

unread,
Apr 16, 2013, 9:25:25 PM4/16/13
to django-social-auth
I think your code was safe enough and that manager won't be creating documents
instance in mongodb, but I recommend to replace that inheritance and use just
"object". Let me know your results on those tests and ping me back on any issue
and I'll try to fix it quickly.

Thanks,
Matías

Excerpts from Shrikrishna Holla's message of 2013-04-16 21:47:32 -0300:
> I used CustomUserManager inherited from auth.User because
> http://django-social-auth.readthedocs.org/en/latest/configuration.html#custom-user-model
> said
> that there *must* be a manager for it to work, which should contain
Matías Aguirre (matias...@gmail.com)

Shrikrishna Holla

unread,
Apr 16, 2013, 10:10:21 PM4/16/13
to django-so...@googlegroups.com
Thank you very much Matias! Your help is appreciated!
Reply all
Reply to author
Forward
0 new messages