I'm currently trying to modify the django-podcast module so the podcast's xml file is statically served instead of being generated on every request for it.
I am attempting to rewrite the channel's xml file every time an episode is modified, created, or deleted and to do so I'm using django signals . What I would like to do is something like this...
from django.db.models.signals import post_save, post_delete
from django.template.loader import render_to_string
def update_xml_file(sender, **kwargs):
f = open('channelrss.xml', 'w')
f.write(render_to_string('podcast/show_feed.html', {'object': sender.show}))
f.close()
class Show(models.Model):
...
class Episode(models.Model):
post_save.connect(update_xml_file)
post_delete.connect(update_xml_file)
...
show = models.ForeignKey(Show)
...
The problem I keep running into is that sender.show is a ReverseSingleRelatedObjectDescriptor
and not an actual instance of the Show class. I also tried reloading the sender object using sender.pk as the primary key value like this...
Episode.objects.filter(pk=sender.pk)
but apparently sender.pk returns a property object
and not an integer or string and I don't know how to get it's value, so I guess I have two questions.
How can I retrieve the instance of Show
associated with the Episode
? and what the heck is a property object
and why does sender.pk return it?
Thanks ahead of time for your response!
Josh
You can try:
def update_xml_file(sender, instance=False, **kwargs): f = open('channelrss.xml', 'w') f.write(render_to_string('podcast/show_feed.html', {'object': instance.show})) f.close()
when instance.show.name_field
is name_field
of the model.
I finally figured it out! This issue was due to my lack of knowledge on the arguments being sent to my signal handler.
The sender
argument sent to my handler was actually a class object and not the instance itself. In order to retrieve the instance itself I needed to use kwargs['instance']
and in order to retrieve the Show
instance I simply used kwargs['instance'].show
As a result I think I understand where the property object
issue was coming from. Correct me if I'm wrong, but when trying to access a 'class' object instead of an 'instance of a class' object the properties aren't defined as string or integer values, but rather property objects that need to be defined.
Also as an additional note, the signal.connect() function doesn't need to be defined in the class's model and the way it is written above is somewhat deceiving. The way it's connected above will listen for any object's save or delete signal sent. In order to associate the function with signal's from only Episode objects I should have written it as...
post_save.connect(update_xml_file, sender=Episode)
post_delete.connect(update_xml_file, sender=Episode)
or by using a decorator as shown in Django's signal documentation .
Thanks again for all the help!
Josh
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.