English 中文(简体)
Django模式,具有动态特性
原标题:Django Model with dynamic attributes

我完全是新的方案拟定工作,因此,请原谅可能是一个棘手的问题,但我过去几天来一直在讨论这个问题。

我有两种模式,即照片和Thumbnail。 我试图找到一种容易和动态的方式,为每个照片打上umb子。 我开始履行这一职能(目标——umb),但我喜欢在模式被称作时自动运行(主要是为了让我获得照片,即,一旦有模型,就立即获得图片。

以下是我的模式。 py. 任何在正确方向上的帮助或判断(即使只是“google blah”)都将受到高度赞赏。 感谢。

class Photo(models.Model):
    name = models.CharField(max_length=100)
    original_image = models.ImageField(upload_to= photos )
    caption = models.TextField(null=True, blank=True)
    title_slug = models.SlugField(null=True, blank=True, unique=True)
    rootfilename = models.CharField(max_length=50, editable=False, blank=True)
    num_views = models.PositiveIntegerField(editable=False, default=0)

    def __unicode__(self):
        return self.name

    thumbnails = Thumbnail.objects.all()

    def create_thumbs(self):
        for thumbnail in self.thumbnails:
            fname = (settings.MEDIA_ROOT + self.rootfilename +  _  + thumbnail.name +  .jpg )
            if exists(fname):
                None
            else:
                t_img = Image.open(self.original_image.path)
                t_fit = ImageOps.fit(t_img, (thumbnail.height,thumbnail.width), Image.ANTIALIAS, 0, (0.5,0.5))
                t_fit.save(fname,"JPEG")

    def save(self, *args, **kwargs):
        self.rootfilename = (self.original_image.name).strip( photos/.jpg )
        super(Photo, self).save(*args, **kwargs)
        self.create_thumbs()

    def get_thumbs(self):
        for thumb in self.thumbnails:
            setattr(self, ( get_ +thumb.name+ _url ), ( thumbs/ +self.rootfilename+ _ +thumb.name+ .jpg ))
最佳回答

页: 1 如你采用节省费用的方法,请打电话,自称<>s_thumbs(,然后打电话超级(Photo, Self).init(*args, **kwargs)

或者,你可以研究其他民族解决这一问题的办法,这些问题是 s、jan、age、或容易发生(如两者结合)。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签