Home > database >  Django rest framework function base view how to get object by string not id
Django rest framework function base view how to get object by string not id

Time:05-21

Problem: I trying to path like this --> path('Image/<str>', views.getImage, name='imageCategory'),

to get image filter by category --> http://127.0.0.1:8000/Image/TV

#-->Model.py
from django.db import models


class Post(models.Model):
    Topic = models.CharField(max_length=250, default='')
    Desc = models.CharField(max_length=750, default='')
    Link = models.TextField(default='')

    def __str__(self):
        return str(self.Topic)


class Category(models.Model):
    categoryName = models.CharField(max_length=50, default='')

    def __str__(self):
        return str(self.categoryName)


class PostImage(models.Model):
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, null=True, related_name='post_name')
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, null=True, related_name='category_name')
    images = models.FileField(upload_to='images/')

    def __str__(self):
        return str(self.post)

#-->Serializer.py
from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from .models import Post, PostImage


class BlogSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = '__all__'


class ImageSerializer(ModelSerializer):
    topic_link = serializers.CharField(source='post.Link', read_only=True)
    Category = serializers.CharField(source='category', read_only=True)

    class Meta:
        model = PostImage
        fields = '__all__'

#-->view.py
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Post, PostImage
from .Serializer import BlogSerializer, ImageSerializer
# Create your views here.


@api_view(['GET'])
def getNames(request):
    Blog = Post.objects.all()
    serializer = BlogSerializer(Blog, many=True)
    return Response(serializer.data)


@api_view(['GET'])
def getName(request, pk):
    Blog = Post.objects.get(id=pk)
    serializer = BlogSerializer(Blog, many=False)
    return Response(serializer.data)

#my problem part

@api_view(['GET'])
def getImage(request):
    image = PostImage.objects.get(Category=request.Category)
    serializer = ImageSerializer(image, many=True)
    return Response(serializer.data)

#-->urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('blogs/', views.getNames, name='blog'),
    path('blogs/<pk>', views.getName, name='blog'),
    path('Image/', views.getImage, name='image'),
    path('Image/<str>', views.getImage, name='imageCategory'),
]

CodePudding user response:

First, you need to set the parameter name in urls.py.

urlpatterns = [
    ...
    path('Image/', views.getImages, name='image'),
    path('Image/<str:category>', views.getImage, name='imageCategory'),
]

Here the category should be the categoryName value of the Category model. Next, in views.py, you need to pass that parameter to the getImage function. And also you need to add getImages function for the case of no category.

@api_view(['GET'])
def getImage(request, category):
    image = PostImage.objects.filter(category__categoryName=category)
    serializer = ImageSerializer(image, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def getImages(request):
    image = PostImage.objects.all()
    serializer = ImageSerializer(image, many=True)
    return Response(serializer.data)

Hope it could help.

  • Related