django auth ldap with custom user model

2019-09-14 23:35发布

I'm trying to use django auth ldap (v1.1.7) with a custom user model and, while I have everything else taken care of, I still run into a familiar error message that I just don't know how to fix.

Running python manage.py syncdb returns:

CommandError: One or more models did not validate:
django_auth_ldap.testprofile: 'user' defines a relation with the model 'auth.Use
r', which has been swapped out. Update the relation to point at settings.AUTH_US
ER_MODEL.

My question here is - where can I find django_auth_ldap.testprofile and update the relation, to fix the issue?

Thanks in advance for helping me out.

L.E.: Ok, after a little googleing, i found out that this testprofile is located in django_auth_ldap/models.py and get this: I searched through all of my files and found the file, modified it to point to settings.AUTH_USER_MODEL and still I receive the same error.

Can anyone please help me out with this issue?

1条回答
爷的心禁止访问
2楼-- · 2019-09-15 00:32

This is the part of django_auth_ldap's models.py -

class TestProfile(models.Model):
    """
    A user profile model for use by unit tests. This has nothing to do with the
    authentication backend itself.
    """
    user = models.OneToOneField('auth.User')  # <-- here is the trouble
    is_special = models.BooleanField(default=False)
    populated = models.BooleanField(default=False)

You cannot fix it unless you fix this part of the code and do as said in Django documentation -

Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active User model – the custom User model if one is specified, or User otherwise.

And I wont recommend modifying the 3rd party packages yourself. If you can, go suggest the change to the developer, or send them a pull request. Once it's accepted, update the package.

查看更多
登录 后发表回答