I cannot insert value in the watchlist table because the user foreign key problem. models.py
from django.db import models
from django.contrib.auth.models import User
class watchlist(models.Model):
watchlist_id = models.AutoField(primary_key=True, max_length=10)
stock_add_time = models.DateTimeField(auto_now=True)
stock = models.ForeignKey(stock, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
objects = models.Manager()
def __str__(self):
return self.stock_add_time
class Meta:
db_table = "watchlist"
verbose_name = 'watchlist name'
views.py
from django.contrib.auth.models import User
from .models import watchlist
@login_required(login_url='login')
def watchlist(request):
user = User.objects.filter(id=request.user)
stock_id = stock.objects.get(stock_id='1')
watchlist.objects.create(stock=stock_id, user=user)
return render(request, 'watchlist.html', {})
I also try many time but still cannot solve it. I hope someone can help or give me any suggestion.