如何在django中的模型实例之前保存相关的模型实例?

时间:2017-10-10 08:56:05

标签: python django django-models django-signals django-transmeta

如何在实例模型之前保存相关的模型实例。

这是必要的,因为我想在模型实例save方法下预处理相关模型的实例字段。

我正在研究Django项目,我处于这种情况,我需要运行一些函数,然后将所有相关的实例模型保存在数据库中。

假设我有一个模特

models.py

from . import signals
class Video(models.Model):
    """Video model"""

    title = models.CharField(
        max_length=255,
        )

    keywords = models.ManyToManyField(
        KeyWord,
        verbose_name=_("Keywords")
    )

创建视频模型的新实例时。

我需要  1.首先保存所有相关模型。     一个。如果相关模型为空,则返回空或无  2.然后保存此视频实例。

我尝试使用post_save信号进行此操作,但无法成功,因为无法保证相关模型首先保存模型。

from django.db.models.signals import post_save, pre_delete, m2m_changed
from django.dispatch import receiver

from .models import Video


@receiver(m2m_changed, sender=Video)
@receiver(post_save, sender=Video)
def index_or_update_video(sender, instance, **kwargs):
    """Update or create an instance to search server."""
    # TODO: use logging system
    # Grab the id
    print("Id is", instance.id)
    # Keywords is empty as keyword instance is saved later than this instace.
    keywords = [keyword.keyword for keyword in instance.keywords.all()]
    print(keywords) # [] empty no keywords
    instance.index()


@receiver(pre_delete, sender=Video)
def delete_video(sender, instance, **kwargs):
    print("Delete index object")
    instance.delete()

更新

可以通过抓取post_save信号并等待单位来实现 当related_models保存时,其相关模型将保存在db中 启动序列化过程并创建平面json文件以及模型字段及其相关实例,这样,平面json文件可以索引 进入弹性搜索服务器。

问题是,我们应该在信号处理程序方法中等待多长时间?以及如何知道所有与实例相关的字段都保存在db。

class Video(models.Model):
    def save(self, *args, **kwargs): 
        # 1. Make sure all of its related items are saved in db 
        # 2. Now save this instance in db. 
        # 3. If the model has been saved. Serialize its value, 
        # 4. Serailize its related models fields 
        # 5. Save all the serialized data into index server 

        # The advantage of using this is the data are indexed in real 
        # time to index server. 

        # I tired to to implement this logic using signals, in case of 
        # signals, when the instance get saved, its related models are 
        # not instantly available in the databse. 

       # Other solution could be, grab the `post_save` signals, wait(delay
       # the serialization process) and start the serialization of 
       # instance model and it's related to convert the data to flat json 
       # file so, that it could index in the searching server(ES) in real 
       # time. 


       # until the instance related models get saved and start to 
       # serialize the data when its

2 个答案:

答案 0 :(得分:0)

您可以在保存实例之前覆盖模型的save()方法并保存相关模型(对象)。

答案 1 :(得分:0)

  

顺便说一句,我使用的是django-admin,我没有定义逻辑   一个视图,添加相关的模型实例由django admin

处理

在这种情况下,您可以翻转ModelAdmin调用save_model()save_related()的顺序,因此从Model.save()您可以获得相关字段的更新值,如上所述在this post

class Video(models.Model):

    def save(self, *args, **kwargs): 
        if not self.id:
            super().save(*args, **kwargs)
        all_updated_keywards = self.keywards.all()
        ...
        super().save(*args, **kwargs)


class VideoAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        if not obj.pk: 
            super().save_model(request, obj, form, change)
        else:
            pass 

    def save_related(self, request, form, formsets, change):
        form.save_m2m()
        for formset in formsets:
            self.save_formset(request, form, formset, change=change)
        super().save_model(request, form.instance, form, change)