Django模型:从外键使用字段 [英] Django-models: use field from foreign key

查看:355
本文介绍了Django模型:从外键使用字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为DJango制作一个图像库项目,只是为了它的缘故。而且,我有一个类名为 Gallery 和一个名为 ImageGallery 的类。



名为gallery的类将如下所示:

  (models.Model):
gallery = models.ForeignKey(Gallery,related_name =parent_gallery)
title = models.CharField(max_length = 200)
folder = models.CharField(max_length = 200)#ex:images / gallery / slugify(self.title)

class ImageGallery(models.Model):
gallery = models.ForeignKey(Gallery,related_name =parent_gallery)
title = models.CharField(max_length = 200)
image = models.ImageField(upload_to = self.gallery.folder)

嗯,最后一行代码是我想知道的,如果它可能或任何其他精细替换。



DJango-admin 中,我想添加表 ImageGallery ,而在选择画廊时,我想使用,将图像保存到画廊中指定的文件夹。文件夹字段。



有什么最好的方法来解决这个问题?我没有完成两个班的写作,但我怀疑他们会这样工作。先谢谢你。 :-)

解决方案

FileField.upload_to 定义如下


此属性提供了一种设置上传目录和文件
名称的方式,可以通过两种方式进行设置。在这两种情况下,值将
传递给Storage.save()方法。 ... upload_to也可以是一个可调用的
,如函数。这将被调用来获取上传路径,
包括文件名。这个可调用必须接受两个参数,
返回一个Unix样式的路径(带有斜杠),以传递到
存储系统。两个论点是:


但是 self.gallery.folder 不是可打电话您需要的是沿着该示例中给出的线设置函数

  def get_upload_path(instance,filename):
return'{0} / {1}'。format(instance.gallery.folder,filename)

您的模型将更改为

  image = models.ImageField(upload_to = get_upload_path)


I'm working on an image gallery project in DJango, just for the sake of it. And well, I have a class named Gallery and a class named ImageGallery.

The class named gallery would look like this:

class Gallery(models.Model):
 gallery = models.ForeignKey(Gallery, related_name="parent_gallery")
 title = models.CharField(max_length=200)
 folder = models.CharField(max_length=200) # ex: images/galleries/slugify(self.title)

class ImageGallery(models.Model):
 gallery = models.ForeignKey(Gallery, related_name="parent_gallery")
 title = models.CharField(max_length=200)
 image = models.ImageField(upload_to=self.gallery.folder)

Well, the last line of code is what I want to know if its possible or any other fine replacement for it.

In the DJango-admin I want to be able to add records for the table ImageGallery and when selecting the Gallery I would like to use, the images to be saved to the folder specified in gallery.folder field.

What's the best way to go around this? I haven't completed writing the two classes but I doubt they're gonna work like this. Thank you in advance. :-)

解决方案

The FileField.upload_to is defined as follows

This attribute provides a way of setting the upload directory and file name, and can be set in two ways. In both cases, the value is passed to the Storage.save() method. ... upload_to may also be a callable, such as a function. This will be called to obtain the upload path, including the filename. This callable must accept two arguments and return a Unix-style path (with forward slashes) to be passed along to the storage system. The two arguments are:

But self.gallery.folder isn't a callable. What you need is to setup a function along the lines given in that example

def get_upload_path(instance, filename):
    return '{0}/{1}'.format(instance.gallery.folder, filename)

And your model will change to

image = models.ImageField(upload_to=get_upload_path)

这篇关于Django模型:从外键使用字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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