Django. У объекта 'GroupDetailView' нет атрибута 'object'
Я пытаюсь создать форму AddMembersForm для группы в своем приложении. У объекта 'GroupDetailView' нет атрибута 'object'
class GroupDetailView(DetailView):
model = Group
context_object_name = 'group'
template_name = 'rooms/group_detail.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AddGroupMemberForm(group_id=self.object.id)
context['members'] = GroupMembers.objects.filter(group=self.object) #Download all members
return context
def post(self, request, *args, **kwargs):
form = AddGroupMemberForm(request.POST, group_id=self.object.id)
if form.is_valid():
new_member = form.save(commit=False)
new_member.group = self.get_object()
new_member.save()
return redirect(self.get_object().get_absolute_url())
else:
return self.render_to_response(self.get_context_data(form=form))
-----------------------------------------------------------------------------------
class Group(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='creator_group', on_delete=models.CASCADE)
name = models.TextField(max_length=50)
description = models.TextField(max_length=500)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('group_detail', kwargs={'pk': self.pk})
class GroupMembers(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='membership')
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='group_membership')
role = models.TextField(max_length=30, default='Członek grupy')
time_release = models.DateTimeField(default=timezone.now)
def __str__(self):
return f'{self.user.username} należy do {self.group.name}'
Я хочу добавить пользователей в мои группы (у основателя группы будет такая возможность).