Duplicating a model while searching

Viewed 18

I wrote simple query schare used ListView class:

class HomeView(ListView):
   model = Person
   template_name = "phones/home_page.html"
   context_object_name = 'people'

   def get_queryset(self):
       query = self.request.GET.get('q')
       if query:
           object_list = self.model.objects.filter(Q(name__icontains=query) | Q(surname__icontains=query) |
                                                Q(phone__phone__icontains=query) | Q(mail__mail__icontains=query))
       else:
           object_list = self.model.objects.all()
       return object_list

I used 3 models one of them:

class Phone(models.Model):
   person = models.ForeignKey(Person, editable=False, on_delete=models.CASCADE, related_name='phone')
   phone = models.CharField(max_length=50, blank=True)

One of 'Q' is in ForeignKey. The problem is when I enter '1' for example, it duplicates the model. example I know why is happen but can I reduce the same model to 1

0 Answers
Related