0

如何访问path_after信号中的字段?

楷模:

class Store(models.Model):
    user = models.ForeignKey(User)
    path = models.FileField(upload_to="src")
    path_after = models.FileField(upload_to="files",blank=True, null=True)

class Document(models.Model):
    rootfile = models.ForeignKey(Store)
    content = models.TextField()

信号:

@receiver(post_save, sender=Store)
def my_handler(sender, **kwargs):
    if kwargs['created']:
        document = Document()
        document.rootfile = kwargs['instance']

        #how to open file from path_after field?
        text_file = open("path_after", "r")

        document.content = #should be text from path_after file, this is TextField
        document.save()
4

2 回答 2

2

您进入instance作为对象的信号处理程序,Store它具有该path_after字段。您可以直接访问它:

@receiver(post_save, sender=Store)
def my_handler(sender, **kwargs):
    if kwargs['created']:
        document = Document()
        document.rootfile = kwargs['instance']

        #how to open file from path_after field?
        text_file = open(kwargs['instance'].path_after.path, "r")
        ...
于 2013-05-28T10:33:12.373 回答
1
@receiver(post_save, sender=Store)
def my_handler(sender, instance, *args, **kwargs):
    if kwargs['created']:
        document = Document()
        document.rootfile = instance

        #how to open file from path_after 
        text_file = open(instance.path_after, "r")

        document.content = #should be text from path_after file, this is TextField
        document.save()
于 2013-05-28T10:34:05.070 回答