Поиск по таблице Django
Пишу БД и пытаюсь сделать поиск, столкнулся с проблемой, что по PK ищет нормально, а по FK поиск не происходит. Может что-то не так делаю...
models.py:
from django.db import models
from state.models import State
from organization.models import Organization
from post.models import Post
from resposible.models import Resposible
from walkie_talkie.models import WalkieTalkie
class Accounting(models.Model):
id = models.AutoField(primary_key=True)
id_walkie_talkie = models.ForeignKey(WalkieTalkie, on_delete=models.CASCADE)
id_organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
id_resposible = models.ForeignKey(Resposible, on_delete=models.CASCADE)
id_post = models.ForeignKey(Post, on_delete=models.CASCADE)
id_state = models.ForeignKey(State, on_delete=models.CASCADE)
relevance = models.BooleanField('Актуальность', default=False)
document = models.CharField('Документ подтверждающий приём на себя рацию', max_length=380)
date = models.DateTimeField('Дата')
comments = models.TextField('Комментарий')
def __str__(self):
return self.id
def get_absolute_url(self):
return f'/accounting/{self.id}'
class Meta:
verbose_name = 'Учёт'
verbose_name_plural = 'Учёт'
views.py:
from django.shortcuts import render, redirect
from .models import Accounting
from .forms import AccountingForm
from django.db.models import Q
def accounting_list(request):
search_query = request.GET.get('search', '')
if search_query:
accounts = Accounting.objects.filter(
Q(id__icontains=search_query) or Q(id_walkie_talkie__id__icontains=search_query))
else:
accounts = Accounting.objects.all()
return render(request, "accounting/list.html", {'accounts': accounts})
list.html:
{% extends "main/base.html" %}
{% block content %}
<div class="card-body">
<h4>Список учетов</h4>
<form class="form-inline my-2 my-lg-0" action="{% url 'accounting_list' %}">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Серийник рации</th>
<th scope="col">Организации</th>
<th scope="col">Ответственные</th>
<th scope="col">Пост</th>
<th scope="col">Состояние</th>
<th scope="col">Актуальность</th>
<th scope="col">Документ подтверждающий приём на себя рацию</th>
<th scope="col">Дата</th>
<th scope="col">Комментарий</th>
</tr>
</thead>
<tbody>
{% for accounting in accounts %}
<tr>
<td>{{ accounting.id }}</td>
<td>{{ accounting.id_walkie_talkie }}</td>
<td>{{ accounting.id_organization }}</td>
<td>{{ accounting.id_resposible }}</td>
<td>{{ accounting.id_post }}</td>
<td>{{ accounting.id_state }}</td>
<td>{{ accounting.relevance }}</td>
<td>{{ accounting.document }}</td>
<td>{{ accounting.date }}</td>
<td>{{ accounting.comments }}</td>
<td><a type="button" class="btn btn-warning" href="update/{{ accounting.id }}">Обновить</a>
<a type="button" class="btn btn-danger" href="delete/{{ accounting.id }}">Удалить</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<a type="button" class="btn btn-success" href="create">Добавить</a>
</div>
{% endblock %}