diff --git a/src/backend/InvenTree/common/migrations/0044_note.py b/src/backend/InvenTree/common/migrations/0044_note.py index 68ff194a47..733d8fc201 100644 --- a/src/backend/InvenTree/common/migrations/0044_note.py +++ b/src/backend/InvenTree/common/migrations/0044_note.py @@ -106,4 +106,18 @@ class Migration(migrations.Migration): models.Model, ), ), + # Once the 'Note' model has been created, we can add the foreign key to the 'NotesImage' model + # This will (initially) allow null values, so that existing images are not affected + # After the data migration, we will come back and mark this field as non-nullable + migrations.AddField( + model_name="notesimage", + name="note", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="common.note", + related_name='images', + ), + ), ] diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 2aadccf933..383f2d9f1e 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -1767,42 +1767,6 @@ class NewsFeedEntry(models.Model): ) -def rename_notes_image(instance, filename): - """Function for renaming uploading image file. Will store in the 'notes' directory.""" - fname = os.path.basename(filename) - return os.path.join('notes', fname) - - -class NotesImage(models.Model): - """Model for storing uploading images for the 'notes' fields of various models. - - Simply stores the image file, for use in the 'notes' field (of any models which support markdown). - """ - - image = models.ImageField( - upload_to=rename_notes_image, verbose_name=_('Image'), help_text=_('Image file') - ) - - user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) - - date = models.DateTimeField(auto_now_add=True) - - model_type = models.CharField( - max_length=100, - blank=True, - null=True, - validators=[common.validators.validate_notes_model_type], - help_text=_('Target model type for this image'), - ) - - model_id = models.IntegerField( - help_text=_('Target model ID for this image'), - blank=True, - null=True, - default=None, - ) - - class CustomUnit(models.Model): """Model for storing custom physical unit definitions. @@ -3119,6 +3083,46 @@ class Note( ) +def rename_notes_image(instance, filename): + """Function for renaming uploading image file. Will store in the 'notes' directory.""" + fname = os.path.basename(filename) + return os.path.join('notes', fname) + + +class NotesImage(models.Model): + """Model for storing uploading images for the 'notes' fields of various models. + + Simply stores the image file, for use in the 'notes' field (of any models which support markdown). + """ + + image = models.ImageField( + upload_to=rename_notes_image, verbose_name=_('Image'), help_text=_('Image file') + ) + + user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) + + date = models.DateTimeField(auto_now_add=True) + + model_type = models.CharField( + max_length=100, + blank=True, + null=True, + validators=[common.validators.validate_notes_model_type], + help_text=_('Target model type for this image'), + ) + + model_id = models.IntegerField( + help_text=_('Target model ID for this image'), + blank=True, + null=True, + default=None, + ) + + note = models.ForeignKey( + Note, on_delete=models.SET_NULL, null=True, blank=True, related_name='images' + ) + + class BarcodeScanResult(InvenTree.models.InvenTreeModel): """Model for storing barcode scans results."""