S
S
Sergey Kutylev2015-08-21 22:07:26
Django
Sergey Kutylev, 2015-08-21 22:07:26

How to display children in django?

There is a model

class Person(model.Model)
    last_name = models.CharField(max_length=30, verbose_name='Фамилия')
    first_name = models.CharField(max_length=30, verbose_name='Имя')
    middle_name = models.CharField(max_length=30, verbose_name='Отчество')
    unit = models.ManyToManyField(Unit, verbose_name='Подразделение')
    position = models.ManyToManyField(Position, verbose_name='Должность')
    chief = models.ForeignKey('self', verbose_name='Руководитель', blank='True', null='True', related_name='chiefname')

There is a chief field where the head of this employee is stored, i.e. displaying a boss for each employee is simple.
The essence of the question is how to display descendants of Person, do I need to change the behavior of the queryset?
Those. there is an employee, he has several people in his subordination, you need to display these people in the template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Barabanov, 2015-08-21
@sakutylev

like this in my opinion
PS: only in my opinion a very strange data structure is obtained. A person can be in several departments, in several positions, and the department and position are not connected in any way. but under the direction of only one other person.
I think it makes more sense to do something like

class Person(...):
  ...
  department = models.ManyToManyField('Department', verbose_name='подразделение', throught='PersonInDepartment')

class Department(..):
  chief = models.ForeignKeyField(Person,....)
  ...

class PersonInDepartment(...):
   person = ForeignKey
   department = ForeignKey
   position = ForeignKey

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question