django-restframework detailview issue -
using django-rest-framework trying implement few simple apis.
following code.
issues facing is:
- the products/(?p\d+)/ never executes productsdetailview. list of products irrespective usage of ids in urls.
- but, when remove products/ urls, single product response. but, unfortunately cannot have products, url api removed.
i not sure wrong doing because of issue happenning. please me.
model:
class product(models.model): product_owner = models.foreignkey(user, verbose_name='user') product_imported_via = models.charfield(max_length=200, default="0", null=false, choices=product_import_source, verbose_name='source of import') product_title = models.charfield(max_length=100, null=false, verbose_name='product title') product_description = models.textfield(max_length=250, verbose_name='product description') product_qty = models.integerfield(verbose_name='quantity') product_mrp = models.decimalfield(max_digits=12, decimal_places=2, verbose_name='maximum retail price') product_offer_price = models.decimalfield(max_digits=12, decimal_places=2, verbose_name='selling price') _product_discount_amount = models.floatfield(null=true, editable=false, verbose_name='discount amount', db_column='product_discount_amount') _product_discount_percentage = models.integerfield(null=true, editable=false, verbose_name='disount percentage', db_column='product_discount_percentage') product_sku = models.charfield(max_length=100, null=false, unique=true, verbose_name='sku',help_text='enter product stock keeping unit') product_barcode = models.charfield(max_length=100, null=false, verbose_name='barcode') archive = models.booleanfield(default=false)
serializer:
from rest_framework import serializers .models import product class productsserializer(serializers.modelserializer): class meta: model = product fields = ('id','product_title', 'product_description', 'product_qty', 'product_mrp', 'product_offer_price','product_discount_amount','product_discount_percentage', 'product_sku','product_barcode')
views:
from rest_framework.views import apiview rest_framework.response import response .models import product .serializers import productsserializer django.http import http404 #create views here. class productslistview(apiview): def get(self, request): print('in update list') updates = product.objects.all() serializer = productsserializer(updates, many=true) return response(serializer.data) class productsdetailview(apiview): def get_object(self, pk): try: return product.objects.get(pk=pk) except product.doesnotexist: raise http404 def get(self, request, pk): update = self.get_object(pk) serializer = productsserializer(update) return response(serializer.data)
urls:
from products.views import productslistview, productsdetailview urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^products/', productslistview.as_view()), url(r'^products/(?p<pk>\d+)/', productsdetailview.as_view()), ]
is there reason aren't using '$' signify end of string in routing?
aside that, if intending have list view , individual view each product id, should revisit django routers documentation won't need 2 routing statements achieve this.
Comments
Post a Comment