How to use django class based views for sending json consist of different model querysets -
i have implement autocomplete search 2 different models "destination , regions", should send json response template according query results consist of 2 different querysets.
do think view should use in case? can offer best practice here ?
you build mixin use in listview
. piggyback on various listview features pagination / model / qs creation.
not different building plain generic.base.view
though!
from django.core import serializers class ajaxlistmixin(object): def dispatch(self, request, *args, **kwargs): if not request.is_ajax(): raise http.http404("this ajax view, friend.") return super(ajaxlistmixin, self).dispatch(request, *args, **kwargs) def get_queryset(self): return ( super(ajaxlistmixin, self) .get_queryset() .filter(ajaxy_param=self.request.get.get('some_ajaxy_param')) ) def get(self, request, *args, **kwargs): return http.httpresponse(serializers.serialize('json', self.get_queryset())) class ajaxdestinationlistview(ajaxlistmixin, generic.listview): # ...
you can see how build mixin in model independent way, can reused across destinations , regions model.
Comments
Post a Comment