Django:在models.filefield(upload_to)位置访问主键 [英] Django: Access primary key in models.filefield(upload_to) location

查看:181
本文介绍了Django:在models.filefield(upload_to)位置访问主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用该条目的主键来保存我的文件。

I'd like to save my files using the primary key of the entry.

这是我的代码:

def get_nzb_filename(instance, filename):
    if not instance.pk:
        instance.save() # Does not work.
    name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()
    name_slug = re.sub('[-]+', '-', name_slug)
    return u'files/%s_%s.nzb' % (instance.pk, name_slug)

class File(models.Model):
    nzb = models.FileField(upload_to=get_nzb_filename)
    name = models.CharField(max_length=256)

我知道第一次保存一个对象的主键是不可用的,所以我愿意采取额外的命中来保存对象只是为了获得主键,然后继续。

I know the first time an object is saved the primary key isn't available, so I'm willing to take the extra hit to save the object just to get the primary key, and then continue on.

上述代码无效。它会抛出以下错误:

The above code doesn't work. It throws the following error:

maximum recursion depth exceeded while calling a Python object

我假设这是一个无限循环。调用保存方法将调用 get_nzb_filename 方法,这将再次调用 save 方法,等等。

I'm assuming this is an infinite loop. Calling the save method would call the get_nzb_filename method, which would again call the save method, and so on.

我正在使用最新版本的Django中继线。

I'm using the latest version of the Django trunk.

如何获取主键,以便我可以使用它来保存我上传的文件?

How can I get the primary key so I can use it to save my uploaded files?

更新@muhuk:

我喜欢你的解决方案。你能帮我执行吗?我已将我的代码更新为以下内容,错误是'File'对象没有属性'create'。也许我正在使用您从上下文中写出的内容?

I like your solution. Can you help me implement it? I've updated my code to the following and the error is 'File' object has no attribute 'create'. Perhaps I'm using what you've written out of context?

def create_with_pk(self):
    instance = self.create()
    instance.save()
    return instance

def get_nzb_filename(instance, filename):
    if not instance.pk:
        create_with_pk(instance)
    name_slug = re.sub('[^a-zA-Z0-9]', '-', instance.name).strip('-').lower()
    name_slug = re.sub('[-]+', '-', name_slug)
    return u'files/%s_%s.nzb' % (instance.pk, name_slug)

class File(models.Model):
    nzb = models.FileField(upload_to=get_nzb_filename, blank=True, null=True)
    name = models.CharField(max_length=256)

而不是在我的模型中强制要求的字段,我会在我的Form类中。没有问题。

Instead of enforcing the required field in my model I'll do it in my Form class. No problem.

推荐答案

似乎您需要预先生成文件首先有空文件字段的模型。然后拿起一个并用给定的文件对象保存。

It seems you'll need to pre-generate your File models with empty file fields first. Then pick up one and save it with the given file object.

你可以有一个这样的自定义管理器方法;

You can have a custom manager method like this;

def create_with_pk(self):
    instance = self.create()
    instance.save()     # probably this line is unneeded
    return instance

但是,如果您的任何一个字段是必需的,这将会很麻烦。因为您最初创建一个空对象,所以您无法在模型级别上强制执行必需的字段。

But this will be troublesome if either of your fields is required. Because you are initially creating a null object, you can't enforce required fields on the model level.

create_with_pk 应该是一个自定义管理方法,在你的代码中,这只是一个常规的方法。因此 self 是无意义的。 记录下来的例子都是正确的。

create_with_pk is supposed to be a custom manager method, in your code it is just a regular method. Hence self is meaningless. It is all properly documented with examples.

这篇关于Django:在models.filefield(upload_to)位置访问主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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