简体   繁体   中英

How to update a related model after another model is created?

I need to automatically update my Account 's amount when a Transaction is created.

I have Transaction model's model.py :

class Transaction(models.Model):
    user = models.ForeignKey(User, default=None)
    account = models.ForeignKey(Account, default=None)
    ...

Its serializers.py :

class TransactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Transaction
        fields = ('id', 'user', 'account_id', 'category_id', 'name', 'amount', 'description', 'created_at')

    def create(self, validated_data):
        return Transaction.objects.create(**validated_data)

And its views.py :

class TransactionList(APIView):
    def get(self, request):
        user_id = request.user.pk
        transactions = Transaction.objects.filter(user_id=user_id).order_by('-created_at')
        serializer = TransactionSerializer(transactions, many=True)

        return Response(serializer.data)

    def post(self, request):
        account_id = request.data['account_id']
        category_id = request.data['category_id']
        serializer = TransactionSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save(user=request.user, account_id=account_id, category_id=category_id)

            self.update_account(request)

            return Response(serializer.data, status=HTTP_201_CREATED)

        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

    def update_account(self, request):
        account_id = request.data['account_id']
        category_id = request.data['category_id']
        account = Account.objects.get(pk=account_id)
        category = Category.objects.get(pk=category_id)

        if category.type == 'expense':
            account.amount = (account.amount - int(self.request['amount']))
        else:
            account.amount = (account.amount + int(self.request['amount']))

        # Then what?

I thought of creating a custom method that will be executed within the condition if serializer is valid, that would get the account and category by their id. I could, so far, display the current values they have, such as amount and name , but after that, I don't know what to do. I'm imagining I need to use my AccountSerializer , but I'm not sure.

The easiest way would be to override the save method of your Transaction model:

class Transaction(models.Model):
    user = models.ForeignKey(User, default=None)
    account = models.ForeignKey(Account, default=None)
    ...
    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        super(Transaction, self).save()
        # Update self.account

See Django documentation for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM