0

如何创建这样的东西:

如果我创建MyFile对象,Document对象将自动创建。(rootfile应该ForeignKeyMyFile

class MyFile(models.Model):
    user = models.ForeignKey(User)
    path = models.FileField(upload_to="files")

class Document(models.Model):
    rootfile = models.ForeignKey(MyFile)
    user = models.ForeignKey(User,related_name='doc_owner')
    title = models.CharField(max_length=255)
    description = models.TextField()
    content = models.TextField()
4

1 回答 1

0

使用post_save()信号MyFile,如果它是新的,也创建Document对象。

@receiver(post_save, sender=MyFile)
def my_handler(sender, **kwargs):
    if kwargs['created']:
        #create document object appropriately.
        document = Document()
        document.rootfile = kwargs['instance']
        document.save()
于 2013-05-27T12:24:15.673 回答