1

I am trying to upload some testing data to an app, using the option of the migrations.All the data is storaged in a .Yaml file in the app ans I have some other migrations that runs perfectly uploading all the data.

But this one has a problem. In this model (Transactions) I created 3 self-writen, fields that are calculated when calling to the save() method. This process run perfectly when I sent the data through the View. But when I send it through the migration, fails as if the save method is not overwritten. I don't know what to do to accomplish the upload as a migration.

The migration


    from django.db import migrations
    from django.core import serializers

    def transactions(apps, schema_editor):
        with open('fixtures/transactions.yaml') as trans:
            for obj in serializers.deserialize("yaml", trans):
                t=apps.get_model("acounts", "Transactions")()
               cat=apps.get_model("acounts","Category")
.objects.get(pk=obj.object.category.pk)
                cuen=apps.get_model("acounts", "Acount").objects.get(pk=obj.object.acount.pk)
                print(obj)
                t.tipo=obj.object.tipo
                t.description=obj.object.description
                t.monto=obj.object.monto
                t.date=obj.object.date
                # t.category=obj.object.category
                t.category=cat
                # t.acount=obj.object.acount
                t.acount=cuen
                t.save()

    class Migration(migrations.Migration):

        dependencies = [
            ('acounts', '0002_populate_acounts'),
        ]

        operations = [
            (migrations.RunPython(transactions))
        ]

The Model

class Transactions(models.Model):
    TYPE_CHOICES = (
        ('GASTO', 'Gasto'),
        ('INGRESO', 'Ingreso'),
        )

    tipo = models.CharField(
        choices=TYPE_CHOICES,
        max_length=20
    )

    description=models.CharField(max_length=100)
    monto=models.DecimalField(max_digits=25, 
        decimal_places=2,
        null=False)
    category=models.ForeignKey('Category', 
        on_delete=models.CASCADE,
        null=False)
    acount=models.ForeignKey('Acount',
        on_delete=models.CASCADE,
        null=False)
    date=models.DateField()



    total_USD=models.DecimalField(
        max_digits=25, 
        decimal_places=2,
        editable=False);    
    total_BTC=models.DecimalField(
        max_digits=25, 
        decimal_places=9,
        editable=False);    
    total_BSS=models.DecimalField(
        max_digits=25, 
        decimal_places=2,
        editable=False);
    total_EUR=models.DecimalField(
        max_digits=25, 
        decimal_places=2,
        editable=False);


    created_at=models.DateTimeField(
        auto_now_add=True,
        editable=False)
    updated_at=models.DateTimeField(
        auto_now=True,
        editable=False)


    def save(self):



        cotizations=Currency.objects.all()
        currency=self.acount.currency.name

        in_usd=self.monto/Currency.objects.get(name=currency).price_USD_now
        query_btc=in_usd*Currency.objects.get(name='BTC').price_USD_now
        query_bss=in_usd*Currency.objects.get(name='YEN').price_USD_now
        query_eur=in_usd*Currency.objects.get(name='EUR').price_USD_now

        self.total_USD=in_usd
        self.total_BTC=query_btc
        self.total_YEN=query_bss
        self.total_EUR=query_eur

        super(Transactions, self).save()

        return query_btc;

The Error

    raise utils.IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'total_USD' cannot be null

The same method runs perfect when done trough the view, How could I create a data migration for this model using the overwritten .save() method?

Omar España
  • 125
  • 6
  • You probably want to check out [this thread](https://stackoverflow.com/questions/28777338/django-migrations-runpython-not-able-to-call-model-methods). Seems like duplication. – tbrlpld May 14 '19 at 19:37
  • Possible duplicate of [Django migrations RunPython not able to call model methods](https://stackoverflow.com/questions/28777338/django-migrations-runpython-not-able-to-call-model-methods) – tbrlpld May 14 '19 at 19:38

0 Answers0