K
K
Konstantin Malyarov2018-02-18 23:46:48
Django
Konstantin Malyarov, 2018-02-18 23:46:48

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)

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)

views.py
from django.shortcuts import render

def index(request, username, department=None):
    print(request.user.department)
    return render(request, 'department/index.html', locals())

Outputs in the console:
department.Department.None
Although there is an entry in the associated table.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Animkim, 2018-02-19
@Konstantin18ko

Religion does not allow you to read the first chapter of any Django manual?
user.department.all()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question