How to add relationship to self on User Model
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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)
indjango_python3_ldap.management.commands.ldap_sync_users
and only update the manager field once all users are in DB.@etianen Sorry im a noob, no idea what to do 😦