Django - 如何更新模型save()方法中的字段?

时间:2013-01-09 16:06:10

标签: django django-models django-file-upload

可以更新save()方法中的模型字段吗?

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']

下面的代码行不做任何操作,

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

可以在保存方法中进行更新吗?

给我一​​些线索。

最诚挚的问候,

2 个答案:

答案 0 :(得分:0)

您是否尝试过检查以查看运行更新时dst的内容?为什么不这样做?

self.image=dst

答案 1 :(得分:0)

将超级保存移至save()的末尾,以便在进行更改后更新模型。

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)