4

It is possible to update a model field inside the save() method?

models.py:

class BicycleAdItemKind(MPTTModel):

    image_file_temp_fullpath = ""
    image_file_temp_filename = ""

    def url(self, filename):
        #pdb.set_trace()

        #url = "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, filename)
        url = "MultimediaData/HelpAdImages/ItemKind/Temp/%s" % (filename)
        self.image_file_temp_fullpath = url
        self.image_file_temp_filename = filename
        return url

    def item_kind_image(self):
        return '<img align="middle" src="/media/%s" height="60px" />' % self.image
    item_kind_image.allow_tags = True     

    n_item_kind      = models.CharField(max_length=50) # Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
    parent           = TreeForeignKey('self', null=True, blank=True, related_name='children')
    description      = models.TextField(null=True, blank=True)
    image            = models.ImageField(upload_to=url, null=True, blank=True)
    date_inserted    = models.DateTimeField(auto_now_add=True)
    date_last_update = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if not self.id:
            BicycleAdItemKind.tree.insert_node(self, self.parent)
        super(BicycleAdItemKind, self).save(*args, **kwargs)

        pdb.set_trace()
        # I will move the file from "Temp" folder to the folder with the "Id" number
        from django.core.files.move import file_move_safe
        src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath 
        dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
        new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
        if not os.path.exists(new_directory):
            os.makedirs(new_directory)

            if file_move_safe(src, dst):
                # I will update the field image
                BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
                # Delete the Temp file

    def __unicode__(self):
        return self.n_item_kind

    class MPTTMeta:
        order_insertion_by = ['n_item_kind']

The line of code bellow does not do any action,

BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)

It is possible to do an update inside a save method?

Give me some clues.

Best Regards,

André
  • 24,706
  • 43
  • 121
  • 178

2 Answers2

1

Move the super save to the end of save() so it updates the model after you make your changes.

def save(self, *args, **kwargs):
    if not self.id:
        BicycleAdItemKind.tree.insert_node(self, self.parent)

    pdb.set_trace()
    # I will move the file from "Temp" folder to the folder with the "Id" number
    from django.core.files.move import file_move_safe
    src = settings.MEDIA_ROOT + "/" + self.image_file_temp_fullpath 
    dst = settings.MEDIA_ROOT + "/" + "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, self.image_file_temp_filename)
    new_directory = settings.MEDIA_ROOT + "/MultimediaData/HelpAdImages/ItemKind/%s" % (self.id)
    if not os.path.exists(new_directory):
        os.makedirs(new_directory)

        if file_move_safe(src, dst):
            # I will update the field image
            BicycleAdItemKind.objects.filter(pk=self.id).update(image=dst)
            # Delete the Temp file

    super(BicycleAdItemKind, self).save(*args, **kwargs)
ravishi
  • 3,349
  • 5
  • 31
  • 40
0

Have you tried checking to see what dst is when you run the update? Why not do it like this?

self.image=dst
girasquid
  • 15,121
  • 2
  • 48
  • 58