如何将搜索结果保存到模型

时间:2019-11-28 03:00:09

标签: django

Models.py

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    comment = models.TextField()
    date_posted = models.DateField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    patient = models.ForeignKey(User, on_delete=models.CASCADE, related_name='patient')

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('public:post-detail', kwargs={'pk': self.pk})

HTML搜索

<div class="content-section">
            <form method="GET" action="{% url 'public:home' %}">
                <input name ="q" value="{{request.GET.q}}" placeholder="search..">
                <button class="btn btn-success" type="submit">
                    Search
                </button>
            </form>
    </div>

VIEWS.py

class SearchResultsView(ListView):
    model = User
    template_name = 'all_users/doctor/search.html'

    def get_queryset(self):  # new
        query = self.request.GET.get('q')
        object_list = User.objects.filter(Q(username__icontains=query))
        return object_list

我想将此object_list保存为“患者”模型。如何将搜索结果保存到模型中。 有没有其他方法可以搜索用户并保存到模型中?

0 个答案:

没有答案