0

I want to create a Relational database with auth_user table which is automatically created in django after migrations(database is postgresql). I have created a table with class named order. My models.py looks like this

Models.py

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

class order(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    price = models.IntegerField()

now I want to link the user id with table order with user_id. And my views.py looks like this. views.py

def order_Data(request):
    product = request.POST['product']
    price = request.POST['price']


    orders = order(product=product,price=price)


    orders.save()
    messages.info(request,'Data saved')
    return render(request,'home.html')

I think I have linked tables properly but in user_id of order table is setting as null and giving me error. I dont know what to do now. Please Help me out (Iam also a learner in Django). Thank you in advance.

1 Answer 1

1

You didn’t pass the user to the order

orders = order(product=product,price=price, user=request.user)

Given you have a logged-in user

Sign up to request clarification or add additional context in comments.

3 Comments

That's it???? Is that so simple?? Okay thanks a lot.
Thank you so much sir.....that worked.....you solved my problem
Please accept answer to close the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.