【问题标题】:Create a search API using django tastypie使用 django sweetpie 创建一个搜索 API
【发布时间】:2016-07-22 19:00:21
【问题描述】:

我正在尝试设计一个用于搜索功能的 api。我想要一个在 reactjs 中实现的 API。我想要的是 /api/v1/rent/search/place="place name" 但我没有得到这个。我所做的是

api.py

from rentals.models import Rental,Gallery
from django.core.paginator import InvalidPage
from django.conf.urls import *
from tastypie.paginator import Paginator
from tastypie.exceptions import BadRequest
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
from haystack.query import SearchQuerySet

class SearchResource(ModelResource):
    class Meta:
        queryset = Rental.objects.all()
        resource_name = 'rent'

    def prepend_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/search%s$" % (
                    self._meta.resource_name,
                    trailing_slash()),
                self.wrap_view('get_search'),
                name="api_get_search"
            ),
        ]

    def get_search(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)
        self.throttle_check(request)

        # Do the query.
        sqs = SearchQuerySet().models(Rental).load_all().auto_query(request.GET.get('q', ''))
        paginator = Paginator(sqs, 20)

        try:
            page = paginator.page(int(request.GET.get('page', 1)))
        except InvalidPage:
            raise Http404("Sorry, no results on that page.")

        objects = []

        for result in page.object_list:
            bundle = self.build_bundle(obj=result.object, request=request)
            bundle = self.full_dehydrate(bundle)
            objects.append(bundle)

        object_list = {
            'objects': objects,
        }

        self.log_throttled_access(request)
        return self.create_response(request, object_list)

models.py

class Rental(models.Model):
    city =  models.CharField(_("City"), max_length=255, blank=False,null=True,
        help_text=_("City of the rental space"))
    place =  models.CharField(_("Place"), max_length=255, blank=False,null=True,
        help_text=_("Place of the rental space"))

class Gallery(models.Model):
    rental = models.ForeignKey('Rental', null=True, on_delete=models.CASCADE,verbose_name=_('Rental'), related_name="gallery")
    image = models.ImageField(blank=True,upload_to='upload/',null=True)

我还需要做什么才能实现 api/v1/rent/search/place="place name"(我想从地名搜索)这样的 url?

我收到以下错误

【问题讨论】:

  • 执行此操作时出现错误 api/v1/searchRent/search/?format=json
  • 你得到什么错误?提供整个堆栈跟踪(如果有)
  • 我现在已经在我的问题中附加了错误。

标签: python django tastypie django-1.9


【解决方案1】:

将此添加到 Python 文件的顶部:

from django.core.paginator import InvalidPage

【讨论】:

  • 如果我这样做 localhost:8000/api/v1/rent/q="place name"/?format=json 我得到一个错误。
  • 试试 localhost:8000/api/v1/rent/?format=json&q="place%20name"
  • 当我执行 localhost:8000/api/v1/rent/?format=json&q="california" 时,我不仅得到了名为 california 的地方的结果,还得到了所有地方的结果。
  • 试试:localhost:8000/api/v1/rent/search/?format=json&q="california"
  • "error_message": "page() 接受 1 个位置参数,但给出了 2 个",如果我这样做会得到这个错误
猜你喜欢
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多