Как передать модель для просмотра списка, который является результатом левого соединения с двумя таблицами?
У меня есть представление на основе класса, где модель является классом Contact.Любой контакт может быть моим любимым, и эта информация сохраняется в классе Favorite.Мне нужно передать модель для просмотра, которая состоит из всех контактов и информации о том, является ли этот контакт любимым или нет.Как я могу передать его на просмотр?
Спасибо за ответы.
///Fave model
class Fave(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_faves')
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
fave = models.BooleanField(default=True)
def get_absolute_url(self):
return reverse('contact:favorite')
///Contact model
class Contact(models.Model):
COMPANY = 'C'
PERSON = 'P'
TYPE_CHOICES = (
(COMPANY, 'Company'),
(PERSON, 'Person'),
)
name = models.CharField(max_length=255, blank=True)
type = models.CharField(max_length=1, blank=False, choices=TYPE_CHOICES)
country = models.CharField(max_length=128, blank=True)
state = models.CharField(max_length=128, blank=True)
city = models.CharField(max_length=128, blank=True)
faves = GenericRelation(Fave, related_query_name='faves')
notes = GenericRelation(Note, related_query_name='notes')
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('contact:contact_detail')
///CBV
class ContactListView(LoginRequiredMixin, ListView):
"""View list of companies"""
model = Contact
template_name = 'contact/contact_list'
context_object_name = 'contacts'
def get_context_data(self, **kwargs):
"""TODO faves """
context = super().get_context_data(**kwargs)
context['filter'] = ContactFilter(self.request.GET, queryset=self.get_queryset())
return context