No problem , you can catch this exception in your views.py if you want ..

On Wed, 25 Mar 2020, 12:46 pm omid jahadi, <omidjaha...@gmail.com> wrote:

> It doesn't work ... Actually, ManyToManyField is not null ... I get error
> when i search a keyword that doesn't match with any user! ... For example,
> first_name is 'Omid', when i search 'o' or 'm', search works fine and
> return 'Omid', but, when i search 'k', i get PageNotFound error
>
> On Wednesday, March 25, 2020 at 7:00:00 AM UTC+4:30, Motaz Hejaze wrote:
>>
>> Add null=True to manytomany field
>>
>>
>> On Wed, 25 Mar 2020, 2:02 am omid jahadi, <omidj...@gmail.com> wrote:
>>
>>> Hello everybody! I want to search in a ManyToManyField in the
>>> DetailView. It works fine if a user with the same query exist, but if there
>>> isn't a user, i get page not found error.
>>>
>>> models.py:
>>>
>>> class agents(models.Model):
>>>     agent_type = models.ForeignKey(types, on_delete=models.SET_NULL, 
>>> blank=True, null=True)
>>>     name = models.CharField(max_length=100)
>>>     users = models.ManyToManyField(user_models.users, through='user_agent')
>>>
>>> views.py:
>>>
>>> class AgentDetailView(LoginRequiredMixin, generic.DetailView):
>>>     model = models.agents
>>>     template_name = 'agent/agent_detail.html'
>>>
>>>     def get_queryset(self):
>>>         query = self.request.GET.get('q')
>>>         if query:
>>>             return 
>>> models.agents.objects.filter(Q(users__user__first_name__contains=query)
>>>                                                 | 
>>> Q(users__user__last_name__contains=query)
>>>                                                 | 
>>> Q(users__id_number__contains=query)
>>>                                                 | 
>>> Q(users__mobile__contains=query))
>>>         else:
>>>             return models.agents.objects.all()
>>>
>>> agent_detail.html:
>>>
>>> <h1>name: {{ agents.name }}</h1>
>>>     <form method="GET" action="" id="searchform">
>>>         <input class="searchfield" id="searchbox" name="q" type="text" 
>>> placeholder="Search..."/>
>>>         <button name="search" type="submit" value="{{ request.GET.q 
>>> }}">Search</button>
>>>     </form>
>>>     {% if agents.users %}
>>>         <p><strong>Users:</strong>
>>>             <br>
>>>             {% for users in agents.users.all %}
>>>                 <li>{{ users }}</li>
>>>                 <hr>
>>>             {% endfor %}
>>>         </p>
>>>     {% else %}
>>>         <p>There are no user for this agent in database.</p>
>>>     {% endif %}
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/54061fa4-2412-424a-8887-916dcc10c051%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/54061fa4-2412-424a-8887-916dcc10c051%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/703c8b77-9775-4895-b5cd-a701f7e2d69d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/703c8b77-9775-4895-b5cd-a701f7e2d69d%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHV4E-fE6hwN75O41WGKnZvqPR%3D6A0ZSiWcwSnyOtG8yP-3q0A%40mail.gmail.com.

Reply via email to