Django:为什么我的自定义save_model不起作用?

时间:2014-12-28 12:10:10

标签: python django

这是我的save_model:

    # define the save model
def save_model(self, request, obj, form, change):
    # buchung is the initial value and obj the changed value

    # check if Buchung was added or changed
    if change == True:

        # get the objects
        buchung = Buchung.objects.get(buchung_id = obj.buchung_id)

        # calculate the change of the buchung_amount
        amountChange = obj.buchung_amount - buchung.buchung_amount

        # check if sollKonto has changed
        if obj.buchung_sollKonto_id != buchung.buchung_sollKonto_id:

            # check which type the initial konto is and reverse the buchung
            sollKonto_initial = Konto.objects.get(konto_id = buchung.buchung_sollKonto_id)

            if sollKonto_initial.konto_type == 'A':
                sollKonto_initial.konto_sum = sollKonto_initial.konto_sum - buchung.buchung_amount
            else:
                sollKonto_initial.konto_sum = sollKonto_initial.konto_sum + buchung.buchung_amount

            # save the object
            sollKonto_initial.save()

            # check which type the new konto is and do the buchung
            sollKonto = Konto.objects.get(konto_id = obj.buchung_sollKonto_id)
            if sollKonto.konto_type == 'A':
                sollKonto.konto_sum = sollKonto.konto_sum + obj.buchung_amount
            else:
                sollKonto.konto_sum = sollKonto.konto_sum - obj.buchung_amount

            # save the object
            sollKonto.save()


        # check if habenKonto has changed
        if obj.buchung_habenKonto_id != buchung.buchung_habenKonto_id:

            # check which type the initial konto is and reverse the buchung
            habenKonto_initial = Konto.objects.get(konto_id = buchung.buchung_habenKonto_id)

            if habenKonto_initial.konto_type == 'P':
                habenKonto_initial.konto_sum = habenKonto_initial.konto_sum - buchung.buchung_amount
            else:
                habenKonto_initial.konto_sum = habenKonto_initial.konto_sum + buchung.buchung_amount

            # save the object
            # !!! this line below is what doesn't seem to work !!!
            habenKonto_initial.save()

            # check which type the new konto is and do the buchung
            habenKonto = Konto.objects.get(konto_id = obj.buchung_habenKonto_id)
            if habenKonto.konto_type == 'P':
                habenKonto.konto_sum = habenKonto.konto_sum + obj.buchung_amount
            else:
                habenKonto.konto_sum = habenKonto.konto_sum - obj.buchung_amount

            # save the object
            habenKonto.save()

        # check if buchung_amount has changed
        if amountChange != 0:
            if obj.buchung_sollKonto_id == buchung.buchung_sollKonto_id:
                if sollKonto.konto_type == 'A':
                    sollKonto.konto_sum = sollKonto.konto_sum + amountChange
                else:
                    sollKonto.konto_sum = sollKonto.konto_sum - amountChange
            if obj.buchung_habenKonto_id == buchung.buchung_habenKonto_id:
                if habenKonto.konto_type == 'P':
                    habenKonto.konto_sum = habenKonto.konto_sum + amountChange
                else:
                    habenKonto.konto_sum = habenKonto.konto_sum - amountChange



    # no change, new one added
    else:
        sollKonto = Konto.objects.get(konto_id = obj.buchung_sollKonto_id)
        habenKonto = Konto.objects.get(konto_id = obj.buchung_habenKonto_id)
        if sollKonto.konto_type == 'A':
            sollKonto.konto_sum = sollKonto.konto_sum + obj.buchung_amount
        else:
            sollKonto.konto_sum = sollKonto.konto_sum - obj.buchung_amount
        if habenKonto.konto_type == 'P':
            habenKonto.konto_sum = habenKonto.konto_sum + obj.buchung_amount
        else:
            habenKonto.konto_sum = habenKonto.konto_sum - obj.buchung_amount

    # save the objects
    sollKonto.save()
    habenKonto.save()
    obj.save()

habenKonto_initial之外的所有值都已更改&保存。我尝试了很多不同的东西,但我似乎无法让它发挥作用。请注意,' sollKonto' ,' habenKonto',' sollKonto_initial' &安培; ' habenKonto_initial'在buchung'中有许多ToManyFields。

我的模型如下:

class Konto(models.Model):
    konto_title = models.CharField(max_length=200)
    konto_id = models.AutoField(primary_key=True) #actually AutoField is safer
    konto_anfangsBestand = models.IntegerField(default=0)
    konto_sum = models.IntegerField(default=0)
    konto_types = (
        ('A', 'Aktiv'),
        ('P', 'Passiv')
    )
    konto_type = models.CharField(max_length=1, choices=konto_types)
    def __str__(self):
        return self.konto_title


class Buchung(models.Model):
    buchung_id = models.AutoField(primary_key=True)
    buchung_descr = models.CharField('Description', max_length=128)
    buchung_amount = models.IntegerField(default=0)
    buchung_sollKonto = models.ForeignKey(Konto, related_name='sollKonto')
    buchung_habenKonto = models.ForeignKey(Konto, related_name='habenKonto')
    buchung_date = models.DateTimeField('Buchungsdatum')
    def __str__(self):
        return self.buchung_descr

0 个答案:

没有答案
相关问题