Answer the question
In order to leave comments, you need to log in
Annotation problem?
There are the following models:
class Chat(models.Model):
participants = models.ManyToManyField(Contact, related_name='chats')
messages = models.ManyToManyField(Message, blank=True)
def __str__(self):
return f'{self.pk}'
class Meta:
verbose_name = 'Чат'
verbose_name_plural = 'Чаты'
class Contact(AbstractBaseUser, PermissionsMixin):
'''Кастомная модель пользователя'''
email = models.EmailField(verbose_name='email', max_length = 60, unique = True)
slug = models.SlugField(default='')
first_name = models.CharField(max_length=30, default = '')
last_name = models.CharField(max_length=30, default = '')
phone_number = models.CharField(max_length=11)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add = True)
last_login = models.DateTimeField(verbose_name='last login', auto_now = True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
avatar = models.ImageField(upload_to='user_avatars/%Y/%m/%d', blank=True)
is_active = models.BooleanField(default=False)
status = models.CharField(max_length=100, default='', blank=True)
friends = models.ManyToManyField('self', blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'phone_number', 'slug']
objects = ContactManager()
def __str__(self):
return self.email
def get_url(self):
try:
return self.avatar.url
except ValueError:
return None
class Meta:
verbose_name = 'Контакт'
verbose_name_plural = 'Контакты'
ordering = ['-date_joined']
class ContactManager(BaseUserManager):
'''Мэнэджер кастомного пользователя'''
def create_user(
self,
email,
first_name,
last_name,
phone_number,
slug,
password = None
):
user = self.model(
email=self.normalize_email(email),
first_name=first_name.capitalize(),
last_name=last_name.capitalize(),
phone_number=phone_number,
slug=slug
)
user.set_password(password)
user.save(using = self._db)
return user
def create_superuser(
self,
email,
first_name,
last_name,
phone_number,
slug,
password = None
):
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
phone_number=phone_number,
slug=slug
)
user.set_password(password)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.is_active = True
user.save(using = self._db)
return user
class ContactCustomViewSet(RetrieveUpdateDestroyPermissionViewset):
'''Обзор, обновление и удаление контакта'''
serializer_class = ContactDetailSerializer
permission_classes = [permissions.IsAuthenticated, IsCurrentUser, ]
permission_classes_by_action = {
'retrieve': [permissions.IsAuthenticated, ]
}
def get_queryset(self):
pk = self.kwargs['pk']
contact = get_object_or_404(Contact, id=pk)
queryset = Contact.objects.filter(id=pk).annotate(
is_friend=Count('friends', filter=Q(friends=self.request.user))
).annotate(
num_friends=Count('friends')
).annotate(
current_user=Count('slug', filter=Q(slug=self.request.user.slug))
).annotate(
is_sent=Exists(
AddRequest.objects.filter(
sender=self.request.user,
receiver=contact,
)
)
).annotate(
is_sent_to_you=Exists(
AddRequest.objects.filter(
sender=contact,
receiver=self.request.user
)
)
).annotate(
chat_id=Sum('chats__id', filter=Q(chats__participants=self.request.user))
)
return queryset
class ContactCustomViewSet(RetrieveUpdateDestroyPermissionViewset):
'''Обзор, обновление и удаление контакта'''
serializer_class = ContactDetailSerializer
permission_classes = [permissions.IsAuthenticated, IsCurrentUser, ]
permission_classes_by_action = {
'retrieve': [permissions.IsAuthenticated, ]
}
def get_queryset(self):
pk = self.kwargs['pk']
contact = get_object_or_404(Contact, id=pk)
queryset = Contact.objects.filter(id=pk).annotate(
is_friend=Count('friends', filter=Q(friends=self.request.user))
).annotate(
num_friends=Count('friends')
).annotate(
current_user=Count('slug', filter=Q(slug=self.request.user.slug))
).annotate(
is_sent=Exists(
AddRequest.objects.filter(
sender=self.request.user,
receiver=contact,
)
)
).annotate(
is_sent_to_you=Exists(
AddRequest.objects.filter(
sender=contact,
receiver=self.request.user
)
)
)
# .annotate(
# chat_id=Sum('chats__id', filter=Q(chats__participants=self.request.user))
# )
return queryset
Answer the question
In order to leave comments, you need to log in
Chat.objects.filter(participants=request.user).filter(participants__id=friend_id_from_url)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question