Answer the question
In order to leave comments, you need to log in
How to get many to many django?
There are two models:
user
class User(AbstractUser):
patronymic = models.CharField(max_length=100, blank=True, null=True)
department = models.ManyToManyField(Department)
class Department(models.Model):
name = models.CharField(max_length=100)
abbreviation = models.CharField(max_length=100, blank=False, null=False)
description = models.TextField(max_length=500, blank=True, null=True)
type_of_department = models.ForeignKey(TypeOfDepartment, default=None, on_delete=models.CASCADE)
from django.shortcuts import render
def index(request, username, department=None):
print(request.user.department)
return render(request, 'department/index.html', locals())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question