Skip to content Skip to sidebar Skip to footer

Manytomany Field Refere To Itself

How can make my model so that its ManyToMany Refer to User class User(AbstractUser): teacher_or_student = models.CharField(max_length=100) mobile_number = models.CharField(

Solution 1:

You can pass the 'self' string for this. By default a ManyToManyField that refers to itself, is als symmetrical, so you probably want to turn that off, since if a is a student of b, then b is not per se a student of a. You can do that by specifying symmetrical=False [Django-doc]:

class User(AbstractUser):
    teacher_or_student = models.CharField(max_length=100)
    mobile_number = models.CharField(max_length=100)
    grade = models.CharField(max_length=100)
    laptop_yes_or = models.CharField(max_length=100)
    students = models.ManyToManyField(
        'self',
        symmetrical=False,
        related_name='teachers'
    )

Post a Comment for "Manytomany Field Refere To Itself"