English 中文(简体)
数据库前的程序领域
原标题:Process field before database insert / update

我有一个Django模式,具有一个文本领域。 在行政站点使用丰富的文字编辑(文稿),使客户能够轻易进入外地。 我愿处理实地的内容,并在数据库中插入任何内容之前采取一些行动。

例如,我要 strip下MS Word、font tags等产生的陪审团。 我希望这部分工作应该容易,但我不敢确定哪些工作要凌驾于主宰或ook。

我也想发现遥控图像,将当地拷贝下载到MEDIA_ROOT,并将 im与当地形象重新联系起来。 我不敢肯定如何绘制边远形象;我想到的是<代码>django。 存储可能有助于,但它认为它无法从遥远的URL中选取内容。

任何建议?

最佳回答

选择陪审员,应当采用习俗形式。

下载图像......有多种办法解决这一问题。

  • If you choose to store the image location and original location in the database, than you should do it with a pre-save signal.
  • If you choose to store the images locally directly, than you can make it part of the formfield aswell. Simply download all remote images and replace the urls with a local url.
问题回答

在保留数据之前,使用<代码>save(<>>/code>方法,如:

def save(self):
  self.NameOfTextField = myCustomCleanFunction(self.NameOfTextField)
  super(YourModelName, self).save()

直至执行<代码>超级(模版名称、自称)save(为止。

如果你想要提出某种类型的错误,而不是沉默地处理,那么你很可能希望使用<条码>.clean()和<条码>的<条码>方法>。

下载远距离内容对我来说是一个新内容,因此我可以在那里帮助你。 你也许必须看一下过去Django的情况,找到做这项工作的灰色功能。

对我的工作


class MyModel(models.Model):
   text = model.TextField()

   def save(self, *args, **kwargs):
        self.text= myCustomCleanFunction(self.text)
        super().save(*args, **kwargs)  

如果程序简单明了,你就可以将守则置于“储蓄职能”而不是“密码”的功能。





相关问题
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 ]="...

热门标签