Rest framework

28 views
Skip to first unread message

Soumen Khatua

unread,
Aug 22, 2019, 4:08:38 AM8/22/19
to django...@googlegroups.com
Hi Folks,

I want to give some sequence of suggestion after type of any character(of course if that character match any sequnce of string in database record) in search query parameter, How I can do that by using Django Rest framework.
Plase help me guys.


Thank You

Regards,
Soumen

Ahmed Shahwan

unread,
Aug 22, 2019, 8:13:47 AM8/22/19
to Django users
you'll just create an endpoint that receives a query parameter with the string to search for, and you'll search using YOURMODEL.objects.get(field=query_params.get("search_str"))

the frontend will be responsible for making the requests to search while typing
so, in your views.py you'll do something like this:



from rest_framework.views import APIView
from rest_framework.response import Response


from .models import YourModel


class Search(APIView):
   
def get(self, request):
        search_str
= request.query_params.get("search_str")
        results
= YourModel.objects.get(field_to_search=search_str)
       
return Response({"results": results})



you'll tie it to a route in urls.py file

and you're good to go

Soumen Khatua

unread,
Aug 22, 2019, 8:32:52 AM8/22/19
to django...@googlegroups.com
Hi Ahmed,
Here is my code:


Views.py
class DatasetListAPIView(ListAPIView):
    serializer_class = DatasetlSerializer
    def get_queryset(self):
        qs = Dataset.objects.all()
        word = self.request.query_params.get('word')
        if word is not None:
            qs  = qs.filter(name__icontains = word).order_by(Length('name').asc(),'-hits')
        return qs
    search_fields = ('name',)


but if my end user type something like:


then I want to show some database match record suggestion like:


I think I need to override inbuilt HTML and Javascript,But I don't know How to do that Please tell me How I can do that. 

Thank You for your response



Regards,
Soumen














--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c6c4f40c-3a2f-4dfc-a310-8e7f17a79827%40googlegroups.com.

Soumen Khatua

unread,
Aug 28, 2019, 6:37:25 AM8/28/19
to django...@googlegroups.com
Hi Folks,
 I have three models like Company,Employee and images. Images is foreignkey in my Company table. At the time of creating Company deatils I want to add image also, How I add these image in my image tables and at the time to get method how i can show company details along with image using django rest framework.

models.py

class Image(models.Model):
    company_image =  models.ImageField(upload_to='company_image',default = 'default.jpg')
    created_at = models.DateField(auto_now_add = True)
    updated_at = models.DateField(auto_now = True)
    class Meta:
        verbose_name = 'image'

    def  __str__(self):
        return self.company_image

class Company(models.Model):
    cmp_image = models.ForeignKey(Image,related_name = 'comp_image',on_delete =                                                                                    models.CASCADE)
    name = models.CharField(max_length = 100)
    reg_no = models.IntegerField()
    ceo = models.CharField(max_length = 100)
    location = models.CharField(max_length = 100)
    active = models.BooleanField(default=True)
    created_at = models.DateField(auto_now_add = True)
    updated_at = models.DateField(auto_now = True)

    def __str__(self):
        return self.name



Thank You

Regards,
Soumen


Andréas Kühne

unread,
Aug 28, 2019, 8:38:29 AM8/28/19
to django...@googlegroups.com
Hi,

This is possible if you have a nested serializer that allow creation. We have created a plugin for that :

There are several others out there that do this also - so it's completely possible.

However when looking at your code  - I think the relationship isn't correct? The way you have written your code - the image is the main object and the company is secondary to it. So when you delete the image, the company will also be deleted - and you can add several companies to the same image, and not the other way around. If that is the way you want it then it's fine. But I would have a foreign key in the image to the company.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Soumen Khatua

unread,
Aug 28, 2019, 9:30:21 AM8/28/19
to django...@googlegroups.com
Hi Andres,
Thank you for your suggestions,Yes the database architecture is wrong. Now I'm doing the practise to all the possible way to solve this problem. But how I can use this plugin to solve problem meanwhile Do you have any documentation for this plugin.

Thank You

Regards,
Soumen

Andréas Kühne

unread,
Aug 28, 2019, 1:03:16 PM8/28/19
to django...@googlegroups.com
All of the code is documented and there should be some readme to help you get started.

Regards,

Andréas


Soumen Khatua

unread,
Aug 28, 2019, 1:12:36 PM8/28/19
to django...@googlegroups.com
Thanks for your support.I got that,now I'm trying to figure out the coding convention.


Thank You

Regards,
Soumen




Reply all
Reply to author
Forward
0 new messages