如何删除重复的django view.py

时间:2019-01-22 03:21:42

标签: python django

我在查询中得到重复的代码,如何将2个变量合并为1行

def destroy(self, request, pk=None):
    date_time = datetime.now()
    user = get_object_or_404(User, pk=request.user.id)
    transaction = TransactionVariant.objects.filter(transaction__pk=pk).update(deleted_at=date_time)
    transaction = Transaction.objects.filter(id=pk).update(deleted_at=date_time)
    serializer = TransactionSerializer(transaction, many=True)
    return Response(serializer.data, many=True)

我的模特:

transaction.py

class Transaction(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    address = models.TextField()
    status = models.CharField(max_length=100)
    total = models.BigIntegerField()
    phone_number = models.CharField(max_length=10)
    created_at = models.DateTimeField(auto_now_add=True, blank=True)
    updated_at = models.DateTimeField(auto_now_add=True, blank=True)
    deleted_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = "transaction"

transaction_variant.py

class TransactionVariant(models.Model):
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
    variant = models.ForeignKey(Variant, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True, blank=True)
    updated_at = models.DateTimeField(auto_now_add=True, blank=True)
    deleted_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = "transaction_variant"

0 个答案:

没有答案