python


How to use a post_save receiver with django-polymorphic?


I have a django-polymorphic model and want to implement a post_save signal to automatically create a related model that is also polymorphic.
It's something like the code below, the relevant piece of non-working code being the #receiver decorated work_post_save method. The problem is the instance is a ctype_id and not an object.
from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from polymorphic.models import PolymorphicModel
from mygallery.models import Album
# Work Parent PolymorphicModel
class Work(PolymorphicModel):
title = models.CharField(blank=True,max_length=256)
slug = models.SlugField(max_length=256)
#receiver(signals.post_save, sender=Work)
def work_post_save(sender, instance, signal, created, **kwargs):
album, new = Album.objects.get_or_create(title=instance.title + ' Stills', slug=instance.slug + '-stills')
work_album, new = WorkAlbum.objects.get_or_create(work=instance, album=album, is_key=True)
class ArtProject(Work):
manifesto = models.CharField(blank=True,max_length=256)
class CodeProject(Work):
code = models.CharField(blank=True,max_length=256)
# Content Parent PolymorphicModel
class WorkContent(PolymorphicModel):
is_key = models.BooleanField(default=False, unique=True, default=False)
class WorkAlbum(WorkContent):
work = models.ForeignKey(Work, related_name='work_albums')
album = models.ForeignKey(Album, related_name='album_works')
I've only fiddled around with this for a bit so I'm not 100% sure what the correct way of handling this is.
What I ended up doing is to not declaring the sender in the #receiver annotation. This has the effect that the callback is triggered by every post_save signal. Then in the callback I check isinstance() with my parent model (in your case Work), so that the callback is only executed after a model I'm interested in is saved. When the callback is executed, the instance parameter is a child model (in your case ArtProject or CodeProject).
#receiver(signals.post_save)
def work_post_save(sender, instance, signal, created, **kwargs):
if isinstance(instance, Work):
# instance is either ArtProject or CodeProject
album, new = Album.objects.get_or_create(title=instance.title + ' Stills', slug=instance.slug + '-stills')
work_album, new = WorkAlbum.objects.get_or_create(work=instance, album=album, is_key=True)
Triggering directly on the parent save() is apparently not supported.

Related Links

Plotting a simple graph
Knowing an item's location in an array [duplicate]
Getting the number of digits of nonnegative integers (Python) [duplicate]
How to mask clouds from Python numpy array for a linear regression model?
How to insert an item into a sublist if sublist is a certain length?
Can't get un-stacked bar plot in python pandas
how can i use python to deploy proxies from the command line
HTMLTestRunner not working
How can I sum values in a list? [duplicate]
Python Dictionary Sorting Using Sorted
xlwt: 'CommonToken' object has no attribute 'txt'
Python repeat while user enters negative number
Assign different operators during python loop
When installing through pip, how do I tell matplotlib how to find tkinter?
Regex for weekdays in python [closed]
Detect Changes to Environment Variables

Categories

HOME
swift
batch-processing
ibm-watson-cognitive
elixir
tinyos
aem
httpclient
desktop
shipping
dropbox
ipfs
unity2d
keystore
android-source
hashicorp-vault
flann
lstm
mule-studio
grub2
derived
jsonserializer
jquery-scrollify
geopandas
google-api-dotnet-client
thinking-sphinx
android-vpn-service
npm-shrinkwrap
dql
powermta
resuming-training
php-ews
vcf
taskmanager
ogre3d
udev
slidesjs
pluck
google-maps-ios
nuget-server
adler32
createobject
amf
image-editing
asp.net-mvc-partialview
twgl.js
asynccallback
fortran90
git-rebase
insert-into
affix
vips
qbfc
risk-analysis
digits
gray-code
apache-mina
scrollspy
mdm-zinc
start-job
gmaps4rails
kendo-mvvm
formvalidation-plugin
httpcontext
visualstatemanager
resource-files
roxygen
angulartics
magicalrecord-2.2
image-zoom
domain-calculus
n-layer
datarepeater
tfs-power-tools
getusermedia
jpf
nintendo
easygui
email-spec
open-graph-beta
j-interop
sitemappath
boost-date-time
oggvorbis
act
revision
hmacsha1
evb
economics
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App