Django - 保存新对象时如何获取self.id? [英] Django - How to get self.id when saving a new object?

查看:487
本文介绍了Django - 保存新对象时如何获取self.id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个模型中有一个问题。我正在上传一个图像,我想存储id(pk在数据库表中),但是我需要知道Django在哪里可以访问 self.id



models.py

 类自行车AdItemKind(MPTTModel):
def url(self,filename):
pdb.set_trace()

url =MultimediaData / HelpAdImages / ItemKind /%s /%s %(self.id,filename)
return url

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

#Bicicleta completa,Componentes para bicicleta,Acessorios para ciclista
n_item_kind = models。 CharField(max_length = 50)
parent = TreeForeignKey('self',null = True,
blank = True,related_name ='children')
description = models.TextField(null = BLAN k = 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 __unicode __(self):
return self.n_item_kind

class MPTTMeta:
order_insertion_by = ['n_item_kind']

问题出在 url()在更新对象时,我只能得到 self.id ,当创建一个对象时,我不会得到 self.id 新的对象。如何修改此模型,以便在创建新对象时获取 self.id



使用当前代码,当我创建一个新对象时,我将最终得到一个如下所示的URL:

  MultimediaData / HelpAdImages / ItemKind / None /somefile.jpg 

我需要有一些类似的东西:

  MultimediaData / HelpAdImages / ItemKind / 35 / somefile.jpg 

任何线索?

解决方案

如果是新对象,则需要先保存,然后访问自身。 id,因为

 在调用save()之前,没有办法知道ID的价值是什么,
,因为该值是由数据库计算而不是由Django计算的。

检查django的文档https://docs.djangoproject.com/en/dev/ref/models/instances/


I have a problem in one of my models. I'm uploading an image, and I want to store the id (pk in the database table) but I need to know at which point Django will have access to self.id.

models.py

class BicycleAdItemKind(MPTTModel):
    def url(self, filename):
        pdb.set_trace()

        url = "MultimediaData/HelpAdImages/ItemKind/%s/%s" % (self.id, 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     

    # Bicicleta completa, Componentes para bicicleta, Acessorios para ciclista
    n_item_kind      = models.CharField(max_length=50) 
    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 __unicode__(self):
        return self.n_item_kind

    class MPTTMeta:
        order_insertion_by = ['n_item_kind']

The problem is in the url() method; I can only get self.id when updating an object, I don't get the self.id when creating a new object. How can I modify this model so that I get self.id when creating a new object?

With the current code, when I'm creating a new object I will end up with a url like:

MultimediaData/HelpAdImages/ItemKind/None/somefile.jpg

And I need to have something like:

MultimediaData/HelpAdImages/ItemKind/35/somefile.jpg

Any clues?

解决方案

If it's a new object, you need to save it first and then access self.id, because

"There's no way to tell what the value of an ID will be before you call save(), 
 because that value is calculated by your database, not by Django."

Check django's document https://docs.djangoproject.com/en/dev/ref/models/instances/

这篇关于Django - 保存新对象时如何获取self.id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆