如何创建这样的东西:
如果我创建了MyFile
个对象,则会自动创建Document
个对象。(rootfile
应为ForeignKey
到MyFile
)
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()
答案 0 :(得分:0)
在MyFile
上使用post_save()
信号,如果是新的,请创建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()