我正在尝试创建一个Django应用程序,允许用户关注特定的股票行情,并根据他们所关注的内容查看相关新闻文章。因此,如果用户要关注“APPl”,“AMZN”和“NFLX”,他们会看到Apple,Amazon和Netflix的新闻文章。我不确定如何将某些Article
连接到某些Stock
或如何将某些Stock
连接到specfic Profile
。在此先感谢您的帮助。我现在的模型如下。
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
followed_tickers = ?
class Stock(models.Model):
ticker = models.CharField(max_length = 50)
class Article(models.Model):
ticker = models.ForeignKey(Stock, on_delete=models.CASCADE)
title = models.CharField(max_length = 200)
url = models.URLField()
description = models.TextField()
publication_date = models.DateTimeField()
def __str__(self):
return self.title
答案 0 :(得分:0)
followed_tickers = models.ManyToManyField(Stock, blank=True)
然后你可以得到股票用户跟随:
stocks = user_obj.followed_tickers.all()
基于Stock用户的文章如下:
articles = Article.objects.filter(ticker__in=stocks)