我有一个存储团队和成员的模型。我想检查所请求的用户是否在此团队中。我该怎么做?
模型
private static readonly ConcurrentQueue<ProductDto> ProductBuffer = new ConcurrentQueue<ProductDto>();
private async void On_ProductReceived(object sender, ProductReceivedArgs e)
{
ProductBuffer.Enqueue(e.Product);
if (ProductBuffer.Count >= handlerConfig.ProductBufferSize)
{
var products = ProductBuffer.ToList();
ProductBuffer.Clear();
await SaveProducts(products);
}
}
模板
class TeamPlayer(models.Model):
team = models.ForeignKey(Team, related_name='players', on_delete=models.CASCADE)
player = models.OneToOneField(User, related_name='player', on_delete=models.CASCADE)
approved = models.BooleanField(default=False)
查看只是简单的 DetailView 。
答案 0 :(得分:0)
如果你想在模板中这样做,只需使用如下的teamplayer_set:
{% if request.user in object.teamplayer_set.all %}
答案 1 :(得分:0)
你有1对1,所以它应该是
players = TeamPlayer.object.all()
if request.user.player in players
# Do what you need to
或在模板中
{% if request.user.player in players %}
提供players
已发送到模板
当它一对一时,它总是user.player
为相关用户提供player.user
。