如何在django中过滤一个事务模型

rseugnpd  于 2023-03-09  发布在  Go
关注(0)|答案(1)|浏览(128)

我正在尝试在两个用户之间进行转换。
models.py

class Trasaction(models.Model):
    sender = models.ForeignKey(
        Account, on_delete=models.PROTECT
    )
    receiver = models.ForeignKey(
        Account, on_delete=models.PROTECT
    )
    amount = models.IntegerField()
    purpose = models.CharField(max_length=100, null=True, blank=True)

    date   = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.sender.username}"

我想查询用户是发送者或接收者的所有事务。
views.py

def transactions_log(request):
    user = request.user
    transactions = Transactions.objects.filter #am stuck
    return render(request, "trasaction.html")
njthzxwz

njthzxwz1#

尝试使用Q对象,如下所示:

from django.db.models import Q

def transactions_log(request):
    user = request.user
    transactions = Transaction.objects.filter(Q(sender=user) | Q(receiver=user))
    return render(request, "transaction.html", {"transactions": transactions})

相关问题