I am working on an e-commerce project, and I want to add products from the front end. The files in my product app in Django is as follows this is my model.py:
class Product(models.Model):
pub_date = models.DateTimeField(default=datetime.now)
price = models.DecimalField(max_digits=100000,decimal_places=5,null=False,blank=False)
device_name = models.CharField(max_length = 50, null=True)
photo = models.ImageField(upload_to = 'product_photos/%y/%m/%d',null=True)
and this is my views.py
@api_view(['POST'])
@permission_classes ([AllowAny] , )
def addproduct(request,pk):
data = request.data
product = Product(
price = data['price'],
device_name = data['product_name'],
photo = data['photo'],
user = Users.objects.get(id=pk)
)
if product:
product.save()
serializer = ProductSerializer(product,many=False)
return Response(serializer.data)
else:
return Response({'error':'4'})
It works fine when it comes to fetching data (products I created in the Django admin) but how to send a JSON request that also contains the images and the other data ,NOTE: my views is function_based