0

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.

  • The function named `watchlist` is replacing the class named `watchlist`. Rename the function, so that `watchlist.objects.create(...)` is referring to the class instead of the function. – Neil May 06 '23 at 04:09
  • Thank you so much your suggestion. My problem is solved from your suggestion. – wai siang May 06 '23 at 04:16

0 Answers0