How can I insert values into database which will retrieves from Category, Seller and Origin model in django?

'''I have made the foreign key relationship in product attributed with Category, Seller, and Origin but could not save the retrieve the values '''

models.py

from django.db import models
from django.contrib.auth.models import User
#from django.db import Seller
# Create your models here.

class Category(models.Model):
    category_name = models.CharField(max_length=10)

    def __str__(self):
        return self.category_name


class Origin(models.Model):
    origin_name = models.CharField(max_length=10,default=1)
    
    def __str__(self):
        return self.origin_name


class Seller(models.Model):
    seller_name = models.OneToOneField(User, on_delete=models.CASCADE,default=1)

    def __str__(self):
        return self.seller_name.username


class Product(models.Model):
    product_code = models.CharField(max_length=20)
    product_name = models.CharField(max_length=20)
    product_category = models.ForeignKey(Category, on_delete=models.CASCADE,null=True,related_name='cat',blank=True)
    product_quantity = models.PositiveBigIntegerField(null=True)
    product_seller = models.ForeignKey(Seller, on_delete=models.CASCADE,null=True,related_name='seller',blank=True)
    product_purchase_price =models.PositiveIntegerField(null=True)
    product_selling_price = models.PositiveIntegerField(null=True)
    product_origin = models.ForeignKey(Origin, on_delete=models.CASCADE,null=True,related_name='origin',blank=True)
    #product_image = m
    #profit_percentage = models.PositiveIntegerField(null=True)

    def __str__(self):
        return self.product_name

''' Here's the views.py vile. Can you please tell me where I have to change? I want to insert the data without using the forms.py file. '''

views.py

from django.http.response import HttpResponse
from django.shortcuts import render
from django.contrib.auth.models import User
from .models import Product,Category, Seller, Origin
from django.contrib.auth import authenticate, login, logout
# from .models import *
# Create your views here.

def add_product(request):
    cat=Category.objects.all()

    sell=Seller.objects.all()
    ori=Origin.objects.all()
    if request.method=="POST":
        product_code=request.POST['product_code']
        product_name=request.POST['product_name']
        product_category=request.POST.get('product_category')
        product_quantity=request.POST['product_quantity']
        product_seller=request.POST.get('product_seller')
        product_purchase_price=request.POST['product_purchase_price']
        product_selling_price=request.POST['product_selling_price']
        product_origin=request.POST.get('product_origin')
        if Product.objects.filter(product_code=product_code).exists():
            return HttpResponse('This Product has already taken. Please Try another Product')
        else:
            new_product=Product(
            product_code=product_code, 
            product_name=product_name,
            product_quantity=product_quantity, 
            product_purchase_price=product_purchase_price, 
            product_selling_price=product_selling_price,
            product_category=product_category,
            product_seller=product_seller,
        
            product_origin=product_origin,
            
            
            )
            new_product.save()
            return HttpResponse('Your Product has successfully Added')
    con={
        'c': cat,
        's': sell,
        'o': ori,
    }
    return render(request, 'product/product_add.html',con)
Back to Top