Есть две модели:
userclass User(AbstractUser):
patronymic = models.CharField(max_length=100, blank=True, null=True)
department = models.ManyToManyField(Department)
departmentclass 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)
views.pyfrom django.shortcuts import render
def index(request, username, department=None):
print(request.user.department)
return render(request, 'department/index.html', locals())
В консоли выводит:
department.Department.None
Хотя в связанной таблице запись есть.