question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add relationship to self on User Model

See original GitHub issue

I extended the user model to add the manager field.

class User(AbstractUser):
    display_name = models.CharField(max_length=500, blank=True)
    title = models.CharField(max_length=255, blank=True)
    company = models.CharField(max_length=255, blank=True)
    manager = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.display_name

But im getting this error ValueError: Cannot assign "'CN=Dy\\, Name,OU=Users,OU=Accounts,OU=***,OU=AP,OU=***,DC=***,DC=***'": "User.manager" must be a "User" instance.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sonicblindcommented, Jul 9, 2019

ldap_sync_users eventually calls django_python3_ldap.ldap.Connection.iter_users()

It loops through all LDAP users and return them as User objects:

django_python3_ldap.ldap.Connection._get_or_create_user()

This method, before returning the User object calls mentioned LDAP_AUTH_SYNC_USER_RELATIONS. By default, this is mapped to django_python3_ldap.utils.sync_user_relations as defined in LDAP config: LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

sync_user_relations does nothing by default. You need to override it. It allows you to define your own actions after a User object has been created or updated.

However, I think that what you want to do is a bit tricky, as you need to make sure that a “manager” User object exists in your DB before you assign this manager’s User object to new user’s User object. And the sync_user_relations runs through LDAP in no such guaranteed order which would ensure that someones manager is always fetched before this user.

I would override the existing class Command(BaseCommand) in django_python3_ldap.management.commands.ldap_sync_users and only update the manager field once all users are in DB.

0reactions
DanSantoDomingocommented, Jul 9, 2019

@etianen Sorry im a noob, no idea what to do 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

django add relationships to user model - python - Stack Overflow
Add a ManytoMany relationship in your article to the User model. Everytime a user likes one article add him into it.
Read more >
Django: How to customise user model and create ... - YouTube
Django: How to create one to one relationship between user and profile model and customise user model to login using email instead of ......
Read more >
Create a Self Relationship with the Position Object - Trailhead
Start by creating a self relationship with the Position object. From Setup, click Object Manager. Click Position. Click Fields & Relationships, then New....
Read more >
Explaining self-referential associations using a friendship model
The self-referential association is very useful when you are trying to build something like a friendship model in your application.
Read more >
4. How to include a self-referencing ForeignKey in a model
Self -referencing foreign keys are used to model nested relationships or recursive relationships. They work similar to how One to Many relationships.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found