#33323: On custom migrations, import of model classes with 
app.get_model('appname',
'ModelName') ignores overwritten models.Model methods
--------------------------------------+------------------------
               Reporter:  ascheucher  |          Owner:  nobody
                   Type:  Bug         |         Status:  new
              Component:  Migrations  |        Version:  3.2
               Severity:  Normal      |       Keywords:
           Triage Stage:  Unreviewed  |      Has patch:  0
    Needs documentation:  0           |    Needs tests:  0
Patch needs improvement:  0           |  Easy pickings:  0
                  UI/UX:  0           |
--------------------------------------+------------------------
 Given migration:

 {{{
 from django.db import migrations
 from designs.models import Design


 def fix_wrong_design_sheet_data(apps, schema_editor):
     # do not import Model classes like in the line below. Overwritten
 methods are not executed.
     # Design = apps.get_model('designs', 'Design')

     for design in Design.objects.all().iterator():
         if design. uuid in ['79f53b09-7069-480e-a8d9-d88ec0aad8d0',
 'e26230cb-a9c7-4ee1-9523-0f2f70cf5c98']:
             design.language = 'nt'
         design.save(
             force_insert=False,
             force_update=True,
             using='default',
             update_fields=['id_slug', 'language'])


 class Migration(migrations.Migration):

     dependencies = [
         ('designs',
 '0027_related_design_cols_to_many_many_remove_old_col'),
     ]

     operations = [
         migrations.RunPython(fix_wrong_design_sheet_data),
     ]
 }}}

 Model class:

 {{{
 class Design(models_mixins.PkUUID, models_mixins.TrackingMixin):
     niche = models.CharField(blank=False, null=True, max_length=50)
     id = models.CharField(blank=False, null=True, max_length=4)
     variant = models.CharField(blank=False, null=True, max_length=1)
     language = models.CharField(max_length=2, default=None)

     # this field is filled by the .save() method
     id_slug = models.CharField(max_length=61, blank=False, null=True)
     ...

     def save(self, force_insert: bool, force_update: bool, using:
 Optional[str], update_fields: Optional[Iterable[str]]) -> None:
         print('new save')
         int_id = int(self.id)
         nslid =
 f'{self.niche}-{int_id:04}-{self.variant}--{self.language}'
         self.id_slug = nslid
         return super().save(force_insert=force_insert,
 force_update=force_update, using=using, update_fields=update_fields)
 }}}

 When imported with `from designs.models import Design`, then the
 Design.save method is called.

 BUG:
 Importing the Design model class with `Design = apps.get_model('designs',
 'Design')` the Design.save method is not called.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33323>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.9d5f0457b6bff2415fd2f1c2b4db1b32%40djangoproject.com.

Reply via email to