简体   繁体   中英

Django models field user and admin from the same model

I have a model User and this other model where the admin and the users are from the same model User.

admin = models.ForeignKey(User)
users = models.ManyToManyField(User)

I get this error:

ERRORS:
admin: (fields.E304) Reverse accessor for 'admin' clashes with reverse accessor for 'users'.
HINT: Add or change a related_name argument to the definition for 'admin' or 'users'.
users: (fields.E304) Reverse accessor for 'users' clashes with reverse accessor for 'admin'.
HINT: Add or change a related_name argument to the definition for 'users' or 'admin'.

Change code like this:

admin = models.ForeignKey(User, related_name='admin_user')
users = models.ManyToManyField(User, related_name='users_user')

Also see ForeignKey related_name

You're creating different users attributes with the same backwards relationships 'user_set', and this doesn't works.

Try it:

admin = models.ForeignKey(User)
users = models.ManyToManyField(User,related_name="+")

provide two different related_name attributes so that the backwards relationship attributes' names don't clash.

In this post, you can see the same problem: Post

The Docs :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM