OrgChart в Django и JQuery
У меня есть приложение, которое я создал на Django и OrgChart jQuery, которое отображает организационную рабочую карту.
У меня есть модель UserProfile, которую я создаю каждый раз, когда создается пользователь, поэтому она обновляется автоматически. Сейчас я работаю над сценарием, который позволил бы мне добавить двух человек на одну и ту же должность и уровень иерархии. Например, я хочу добавить 2 генеральных директора на вершину иерархии, но функция выдает мне ошибку:
MultipleObjectsReturned at /organizational-chart/
get() returned more than one UserProfile -- it returned 2!
views.py
def work_organizational_chart(request):
def recursion(data, profile_id):
new_child_list = []
ro = data.filter(supervisor=profile_id)
for j in ro:
temp = {'profile_id': j.profile_id,
'user_full_name': j.user.get_full_name(),
'email': j.user.email,
'team': j.team.title,
'job_position': j.job_position,
'phone': j.phone_number,
'photo': j.photo.url
}
temp['children'] = recursion(data, temp['profile_id'])
if len(temp['children']) == 0:
del temp['children']
new_child_list.append(temp)
return new_child_list
data_list = UserProfile.objects.filter(profile_id__gte=1)
categories = Category.objects.exclude(title="optimazon")
final_json = {}
cs = data_list.get(team=5)
final_json['profile_id'] = cs.profile_id
final_json['user_full_name'] = cs.user.get_full_name()
final_json['email'] = cs.user.email
final_json['team'] = cs.team.title
final_json['job_position'] = cs.job_position
final_json['phone'] = cs.phone_number
final_json['photo'] = cs.photo.url
final_json['children'] = recursion(data_list, final_json['profile_id'])
return render(request, 'organizational_chart/org_chart.html', {'a_staff': final_json, 'categories': categories})
models.py
class UserProfile(models.Model):
profile_id = models.IntegerField(primary_key=True)
user = models.OneToOneField(User, unique=True, on_delete=models.CASCADE, null=True)
body = models.TextField(verbose_name='Job description', max_length=800, blank=True)
supervisor = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name='supervisor_empl')
job_position = models.CharField(max_length=100, blank=True)
tags = TaggableManager()
photo = models.ImageField(null=True, blank=True, upload_to = "user-images/", default='user-images/206316_J1EvfSL.jpeg')
team = models.ForeignKey(Team, on_delete=models.CASCADE, null=True)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
Вопрос:
Я знаю, что get()
вызывает ошибку, но что я могу использовать вместо get()
в моей функции, чтобы исправить эту ошибку и отобразить 2 CEO без ошибок?