Ad
If I Create MyFile Object, Document Object Will Be Create Automatically
How to create something like this:
If I create MyFile
object, Document
object will be create automatically.(rootfile
should be ForeignKey
to 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()
Ad
Answer
Use post_save()
signal on MyFile
and if its new, create Document
object too.
@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()
Ad
source: stackoverflow.com
Related Questions
- → Django, code inside <script> tag doesn't work in a template
- → Uncaught ReferenceError: Parent is not defined
- → React - Django webpack config with dynamic 'output'
- → Put a Rendered Django Template in Json along with some other items
- → Implement shopify templates in django
- → Python Shopify API output formatted datetime string in django template
- → How to avoid being crawled/penalized by Google
- → Django: Identify the urls that provide duplicate content and set a canonical link
- → Shopify app: adding a new shipping address via webhook
- → Jquery Modal Confirmation on Django form submit for deletion of object
- → changing the size of an image with css
- → shopify_auth multi store session handling
- → How to use Shopify Python API RecurringApplicationCharge
Ad