How to get django-ldapdb to recognize datasource as LDAP
I have followed the directions in the django-ldap README and I cannot seem to get django-ldapdb to act like it's making an LDAP query. The following has been edited on a brand new instance of Django v.2.1.2 using with Python 3.7:
Changes to settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': 'ldaps://my.server',
'USER': 'cn=some user',
'PASSWORD': 'somePassword',
}
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
New models.py
:
class MyPerson(ldapdb.models.Model):
base_dn = "ou=people,dc=ucsf,dc=edu"
object_classes = ['person', 'myPerson]
uid = fields.IntegerField(db_column='uid')
displayname = fields.CharField(db_column='displayname')
eid = fields.CharField(db_column='eid')
def __str__(self):
return str(self.uid)
def _unicode__(self):
return str(self.uid)
The query in my view. First I tried:
result = MyPerson.objects.filter(uid=99894)
Then I tried:
result = MyPerson.objects.using('ldap').filter(uid=99894)
Running the Django dev server in PyCharm's debugger I can see that result
receives a QuerySet
with a message of:
Unable to get repr for <class 'django.db.models.query.QuerySet'>
What do I mean by "message". Honestly I'm not sure, but the debugger shows this:
Also, it seems that though the db
member of the QuerySet
is 'ldap', and the query
member shows an SQL query, not an LDAP filter. As I traced the HTTP request through the URL routing, to the view, to the query, and then the result, I never once saw it make any sort of LDAP-related call. For good measure I mangled the LDAP bind password and I don't get a bind error. Pretty sure I'm missing something that lets Django know I want to work with LDAP at this point... I just don't know what that is.
python django
add a comment |
I have followed the directions in the django-ldap README and I cannot seem to get django-ldapdb to act like it's making an LDAP query. The following has been edited on a brand new instance of Django v.2.1.2 using with Python 3.7:
Changes to settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': 'ldaps://my.server',
'USER': 'cn=some user',
'PASSWORD': 'somePassword',
}
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
New models.py
:
class MyPerson(ldapdb.models.Model):
base_dn = "ou=people,dc=ucsf,dc=edu"
object_classes = ['person', 'myPerson]
uid = fields.IntegerField(db_column='uid')
displayname = fields.CharField(db_column='displayname')
eid = fields.CharField(db_column='eid')
def __str__(self):
return str(self.uid)
def _unicode__(self):
return str(self.uid)
The query in my view. First I tried:
result = MyPerson.objects.filter(uid=99894)
Then I tried:
result = MyPerson.objects.using('ldap').filter(uid=99894)
Running the Django dev server in PyCharm's debugger I can see that result
receives a QuerySet
with a message of:
Unable to get repr for <class 'django.db.models.query.QuerySet'>
What do I mean by "message". Honestly I'm not sure, but the debugger shows this:
Also, it seems that though the db
member of the QuerySet
is 'ldap', and the query
member shows an SQL query, not an LDAP filter. As I traced the HTTP request through the URL routing, to the view, to the query, and then the result, I never once saw it make any sort of LDAP-related call. For good measure I mangled the LDAP bind password and I don't get a bind error. Pretty sure I'm missing something that lets Django know I want to work with LDAP at this point... I just don't know what that is.
python django
you are getting the repr error probably because your str method should return a string:def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17
add a comment |
I have followed the directions in the django-ldap README and I cannot seem to get django-ldapdb to act like it's making an LDAP query. The following has been edited on a brand new instance of Django v.2.1.2 using with Python 3.7:
Changes to settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': 'ldaps://my.server',
'USER': 'cn=some user',
'PASSWORD': 'somePassword',
}
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
New models.py
:
class MyPerson(ldapdb.models.Model):
base_dn = "ou=people,dc=ucsf,dc=edu"
object_classes = ['person', 'myPerson]
uid = fields.IntegerField(db_column='uid')
displayname = fields.CharField(db_column='displayname')
eid = fields.CharField(db_column='eid')
def __str__(self):
return str(self.uid)
def _unicode__(self):
return str(self.uid)
The query in my view. First I tried:
result = MyPerson.objects.filter(uid=99894)
Then I tried:
result = MyPerson.objects.using('ldap').filter(uid=99894)
Running the Django dev server in PyCharm's debugger I can see that result
receives a QuerySet
with a message of:
Unable to get repr for <class 'django.db.models.query.QuerySet'>
What do I mean by "message". Honestly I'm not sure, but the debugger shows this:
Also, it seems that though the db
member of the QuerySet
is 'ldap', and the query
member shows an SQL query, not an LDAP filter. As I traced the HTTP request through the URL routing, to the view, to the query, and then the result, I never once saw it make any sort of LDAP-related call. For good measure I mangled the LDAP bind password and I don't get a bind error. Pretty sure I'm missing something that lets Django know I want to work with LDAP at this point... I just don't know what that is.
python django
I have followed the directions in the django-ldap README and I cannot seem to get django-ldapdb to act like it's making an LDAP query. The following has been edited on a brand new instance of Django v.2.1.2 using with Python 3.7:
Changes to settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'ldap': {
'ENGINE': 'ldapdb.backends.ldap',
'NAME': 'ldaps://my.server',
'USER': 'cn=some user',
'PASSWORD': 'somePassword',
}
}
DATABASE_ROUTERS = ['ldapdb.router.Router']
New models.py
:
class MyPerson(ldapdb.models.Model):
base_dn = "ou=people,dc=ucsf,dc=edu"
object_classes = ['person', 'myPerson]
uid = fields.IntegerField(db_column='uid')
displayname = fields.CharField(db_column='displayname')
eid = fields.CharField(db_column='eid')
def __str__(self):
return str(self.uid)
def _unicode__(self):
return str(self.uid)
The query in my view. First I tried:
result = MyPerson.objects.filter(uid=99894)
Then I tried:
result = MyPerson.objects.using('ldap').filter(uid=99894)
Running the Django dev server in PyCharm's debugger I can see that result
receives a QuerySet
with a message of:
Unable to get repr for <class 'django.db.models.query.QuerySet'>
What do I mean by "message". Honestly I'm not sure, but the debugger shows this:
Also, it seems that though the db
member of the QuerySet
is 'ldap', and the query
member shows an SQL query, not an LDAP filter. As I traced the HTTP request through the URL routing, to the view, to the query, and then the result, I never once saw it make any sort of LDAP-related call. For good measure I mangled the LDAP bind password and I don't get a bind error. Pretty sure I'm missing something that lets Django know I want to work with LDAP at this point... I just don't know what that is.
python django
python django
edited Nov 14 '18 at 0:19
JasonGabler
asked Nov 13 '18 at 23:43
JasonGablerJasonGabler
31529
31529
you are getting the repr error probably because your str method should return a string:def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17
add a comment |
you are getting the repr error probably because your str method should return a string:def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17
you are getting the repr error probably because your str method should return a string:
def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
you are getting the repr error probably because your str method should return a string:
def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17
add a comment |
1 Answer
1
active
oldest
votes
As LDAPdoes not represent a relational database and generally has a schema which is created via configuration and not as it would be with queries, it never dawned on me that I needed to run manage.py makemigrations
and manage.py migrate
. (I'm relatively new to Python, and even more so to Django. Multi-datasource ORMs that I've used and extended for LDAP in the past did not required similar preparations.) On a hunch, I ran the manage.py
commands over my LDAP models and then tried my code again. Now it works.
FWIW - I worked with PHP Symfony for some years and authored ucsf-iam/UcsfLdapOrm
. While Symfony also has a db migration process, as LDAP schema are fairly static, when I wrote that LDAP ORM I hard coded part of what Django migration takes care of on the fly. The rest was taken care of by PHP annotations in model classes, similar to how Django has pythonic field types and relates them to LDAP attribute types. Now that I understand all of this better, I have a deeper appreciation for how Django does ORM setup.
I hope this is instructional for other LDAP developers moving over to Python and Django.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291103%2fhow-to-get-django-ldapdb-to-recognize-datasource-as-ldap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As LDAPdoes not represent a relational database and generally has a schema which is created via configuration and not as it would be with queries, it never dawned on me that I needed to run manage.py makemigrations
and manage.py migrate
. (I'm relatively new to Python, and even more so to Django. Multi-datasource ORMs that I've used and extended for LDAP in the past did not required similar preparations.) On a hunch, I ran the manage.py
commands over my LDAP models and then tried my code again. Now it works.
FWIW - I worked with PHP Symfony for some years and authored ucsf-iam/UcsfLdapOrm
. While Symfony also has a db migration process, as LDAP schema are fairly static, when I wrote that LDAP ORM I hard coded part of what Django migration takes care of on the fly. The rest was taken care of by PHP annotations in model classes, similar to how Django has pythonic field types and relates them to LDAP attribute types. Now that I understand all of this better, I have a deeper appreciation for how Django does ORM setup.
I hope this is instructional for other LDAP developers moving over to Python and Django.
add a comment |
As LDAPdoes not represent a relational database and generally has a schema which is created via configuration and not as it would be with queries, it never dawned on me that I needed to run manage.py makemigrations
and manage.py migrate
. (I'm relatively new to Python, and even more so to Django. Multi-datasource ORMs that I've used and extended for LDAP in the past did not required similar preparations.) On a hunch, I ran the manage.py
commands over my LDAP models and then tried my code again. Now it works.
FWIW - I worked with PHP Symfony for some years and authored ucsf-iam/UcsfLdapOrm
. While Symfony also has a db migration process, as LDAP schema are fairly static, when I wrote that LDAP ORM I hard coded part of what Django migration takes care of on the fly. The rest was taken care of by PHP annotations in model classes, similar to how Django has pythonic field types and relates them to LDAP attribute types. Now that I understand all of this better, I have a deeper appreciation for how Django does ORM setup.
I hope this is instructional for other LDAP developers moving over to Python and Django.
add a comment |
As LDAPdoes not represent a relational database and generally has a schema which is created via configuration and not as it would be with queries, it never dawned on me that I needed to run manage.py makemigrations
and manage.py migrate
. (I'm relatively new to Python, and even more so to Django. Multi-datasource ORMs that I've used and extended for LDAP in the past did not required similar preparations.) On a hunch, I ran the manage.py
commands over my LDAP models and then tried my code again. Now it works.
FWIW - I worked with PHP Symfony for some years and authored ucsf-iam/UcsfLdapOrm
. While Symfony also has a db migration process, as LDAP schema are fairly static, when I wrote that LDAP ORM I hard coded part of what Django migration takes care of on the fly. The rest was taken care of by PHP annotations in model classes, similar to how Django has pythonic field types and relates them to LDAP attribute types. Now that I understand all of this better, I have a deeper appreciation for how Django does ORM setup.
I hope this is instructional for other LDAP developers moving over to Python and Django.
As LDAPdoes not represent a relational database and generally has a schema which is created via configuration and not as it would be with queries, it never dawned on me that I needed to run manage.py makemigrations
and manage.py migrate
. (I'm relatively new to Python, and even more so to Django. Multi-datasource ORMs that I've used and extended for LDAP in the past did not required similar preparations.) On a hunch, I ran the manage.py
commands over my LDAP models and then tried my code again. Now it works.
FWIW - I worked with PHP Symfony for some years and authored ucsf-iam/UcsfLdapOrm
. While Symfony also has a db migration process, as LDAP schema are fairly static, when I wrote that LDAP ORM I hard coded part of what Django migration takes care of on the fly. The rest was taken care of by PHP annotations in model classes, similar to how Django has pythonic field types and relates them to LDAP attribute types. Now that I understand all of this better, I have a deeper appreciation for how Django does ORM setup.
I hope this is instructional for other LDAP developers moving over to Python and Django.
edited Nov 14 '18 at 20:06
answered Nov 14 '18 at 0:48
JasonGablerJasonGabler
31529
31529
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291103%2fhow-to-get-django-ldapdb-to-recognize-datasource-as-ldap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you are getting the repr error probably because your str method should return a string:
def __str__(self): return str(self.uid)
– Uku Loskit
Nov 14 '18 at 0:01
@UkuLoskit , thank you for the suggestion but it made no difference. I understand that the uid member is an integer variabe and users of str are expecting a string. That should be corrected either way. But, what made you think of that in relation to my problem? (btw, changing my original code above to reflect this change)
– JasonGabler
Nov 14 '18 at 0:17