Django:如何将 manytomany 字段(从用户到用户本身)添加到内置用户模型

Django: How to add a manytomany field(from user to user itself) to the built in user model

我想在内置用户模型中添加的示例字段

class bulit_in_user_model(models.Model):
    
    relationships = models.ManyToManyField('self',on_delete=models.CASCADE)

怎么做

在此处阅读有关使用 ManyToManyField.symmetrical 引用自身的更多信息

from django.db import models

class MyUser(models.Model):
    ...
    friends = models.ManyToManyField("self", blank=True)

猴子补丁可以达到你的目的,但最好还是按照正常的方式User model Customization though

继承自 AbstractUser,并在您的设置中指定 AUTH_USER_MODEL = 'myapp.MyUser

class MyUser(AbstractUser):
    friends = models.ManyToManyField("self", blank=True)