How to save Field Value in Django Admin Database into a variable

So here I have a table named Product which contains Food name,Category,Price,Image,Dataset id column so i would like to retrieve the value of Dataset id or Food name and save it in variable of int or string datatype in my views.py file.

Here is my views.py code

from .models import Product
def cart(request):

if request.user.is_authenticated:
print(Product.dataset_id)
res=recommend(item_id=*datset id variable here*, num=3)
customer = request.user.customer
order , created = Order.objects.get_or_create(customer = customer , complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
print(res)

Here is my models.py

```
class Product(models.Model):
food_id = models.AutoField
food_name = models.CharField(max_length=50, default="")
cateory = models.CharField(max_length=50, default="")
subcategory = models.CharField(max_length=50, default="")
price = models.IntegerField(default=0)
image = models.ImageField(upload_to='menu/images', default="")
dataset_id = models.IntegerField(default = 0)

[quote="Radon333, post:1, topic:5639, full:true"]
![KZPGZ|690x367](upload://4h1pTXaI8KMJ6W9OX0jMzQIZDOP.png) 

So here I have a table named Product which contains Food name,Category,Price,Image,Dataset id column so i would like to retrieve the value of Dataset id or Food name and save it in variable of int or string datatype in my views.py file.

Here is my views.py code

    from .models import Product
    def cart(request):

    if request.user.is_authenticated:
    print(Product.dataset_id)
    res=recommend(item_id=*datset id variable here*, num=3)
    customer = request.user.customer
    order , created = Order.objects.get_or_create(customer = customer , complete=False)
    items = order.orderitem_set.all()
    cartItems = order.get_cart_items
    print(res)
 
Here is my models.py

    ```
    class Product(models.Model):
    food_id = models.AutoField
    food_name = models.CharField(max_length=50, default="")
    cateory = models.CharField(max_length=50, default="")
    subcategory = models.CharField(max_length=50, default="")
    price = models.IntegerField(default=0)
    image = models.ImageField(upload_to='menu/images', default="")
    dataset_id = models.IntegerField(default = 0)

More about my project. So this is food ordering web application here and i have made small ml model which gives user food recommendation from past order.When i click on order now in my front end it saves it in orders page and saves in the database.So i need to save dataset id or food name into variable to pass it into ml model function.
[/quote]

See the Models docs, in particular, read the various examples for usage.

Also, when posting code, post it between lines consisting of three ` (backtick) characters. That allows the formatter to make your code look good.
example (the line after this consists only of ```):

class DayForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

The line before this consists only of ```

1 Like