How can i implement advanced search in django? - django

How can i implement advanced search like the one below in Django?
Link
When I searched, I don't want the page to be refresh.
What is the best practice for doing that? Ajax, rest framework, or other things...?
Is there a reference?

This question is very vague, so here is an overview of the main steps:
On the frontend, have a form with all the desired filters
On filter update or on form submit, send a GET request to the Django server with all of your parameters encoded in the URL (like a typical GET request)
Then, on the server-side (ie Django), assuming you are using Django Rest Framework:
Create a view/action (through an API View or a Viewset) for your filter research
Extract the filters sent by the frontend which will be located in request.query_params
Perform a request in your database based on those filters:
You know what filters are expected
For each one, if it is not empty, perform an additional filter on your model (Model.objects.filter(X=Y))
Then eventually return the results
There are many ways of doing filtering of a model in Django and DRF:
You can use Q to perform complex queries
You can use the django-filters package to easily add filtering in your viewset based on the received GET parameters

Related

Is it good idea to use result of dump_bulk() method from django-treebeard instead serialization in Serializer or ModelSerializer classes in DRF?

I wanna to build e-commerce website with Django Rest Framework and React.
In e-commerce websites there are nested categories. So I needed to build model which can works with hierarchical data.And I stopped at the Django treebeard library.
I've chosen Materialized path algorithm for store hierarchical data.
But I decided to practice with this library before developing my main project and I tried to do serialization with one query to the DB but I failed
In Django treebeard there is dump_bulk() method which get all data from DB in one query (But if I use Nested sets it get all data in multiple queries)
Is it good idea send result of dump_bulk()method with Response() object to client side?

How to build conversational form with django?

I am trying to build a conversational form with Django.
It will be used in landing page. The form questions will be loaded one by one as user answers them. And there will be some greeting and "human-way" responses to user input (such as "wow! you did a good choice!" after user selects one of the choices from form). The experience and look of the app will be like a real-time chat but user can only select one of the choices from form or upload a file/image.
1. Which technology is better to use for it? I am planning to do it with Fetch.
2. Since I want it to work without page reloading, how do I need to load Django forms through Fetch? Do I need to pass elements of it with JSON and construct it in client-side or can I just pass it as an html with {{form.as_p}} and display it in HTML?
Does these options make difference in matter of security?
I do not know anything about Fetch, but anyway I think it must be constructed clientside, but at first I would simply display the form in a template to get the ids of its fields and then use it in clientside code.
What about security - you'll need to pass the csrf token via your form.

Improving the loading performance of foreignkey fields with thousands of options

I have a similar situation as this post in which I encounter a slow page loading as I have thousands of entries in a foreignkey field.
At modelform, is there a way to improve the page loading while keeping the dropdown function? I have used select2 to efficiently find the chosen item in the dropdown, thus want to keep this function.
Django has to fetch all those foreignkey objects from database and then render them as HTML. This is why it takes a lot of time. Fetching from database can be pretty quick if you cache everything, but rendering to HTML will still be a problem.
Here's a solution that I think would work best:
Exclude that ForeignKey field from your form.
Instead, just create a blank input field. You'll have to capture user's input via JavaScript and send that value to your backed to fetch suggestions. In fact, select2 supports fetching data from backend via AJAX, So, half of your work is done.
Create a view which will take that AJAX request and search the database for suggestions and send them back to the client.
And you're done.

How to use Django Rest API in Django normal project for autocomplete using Select2?

In my Django project, I have a model named Product. The model consists of products which have following entities:
name, id, price and so on.
In my project, an admin can add a new/old product anytime.
Now, for searching, I want to add autocomplete. I want to use Select2.
So users don't have to memorize the name of the products. To do that I found out here in the Select2 doc
that :
Select2 comes with AJAX support built in, using jQuery's AJAX methods
With this, I can search an API and fetch the data to show the users in autocomplete search field.
My Question:
Should I create a Django rest API and use that API to store products and fetch the data?
1.1 Would it be wise?
1.2 Is it possible to make a rest API within a normal Django project? If not then how to do that?
Or should I just use a normal urls.py and querying the result from
Select2 ajax function to that urls.py and to a custom query.py and
fetch the data directly from the database?
I don't think using rest framework with normal Django project would cause any problem. You are just adding some extra urls, that's all. It won't cause any problem to your project. Moreover, you can use the API to get json data of various models.
Hope this helps.

Using django RESTful api to get data and show it in a table?

I am using REST api to get data from a mongodb on a server. Currently I can display the data from mongodb on to a browser using django rest framework, but this data is shown in JSON format.
I want to display this data it in a table.
I am still not clear on how to use this data in a template rather than just return a response that throws data onto a browser.Do I use a serializer for this ?
A google search revealed these 2 results:
Displaying a Table in Django from Database
http://django-tables2.readthedocs.org/en/latest/#tutorial
Both the code assume that the model is defined within the Django, but I am using the REST to get data.
I am using class based views and mixins as shown below for processing http requests on
class RestDjango(View, RequestMixin, ResponseMixin):
.........
.........
p.s:
I am using 0.3.3 version of Django REST Framework.
I am new to REST so please feel free to point me to any tutorials/articles to help me.
Do I use a serializer for this ?
No. You use renderer for this.
http://www.django-rest-framework.org/api-guide/renderers/#templatehtmlrenderer
I think the example code in the link is clear enough. Simply pass the query result as context and create your table inside template.