Caching behavior - possibly incorrect
I am using a foreignkey to dbtemplates Template model to determine how to display another model. ie.
class MyModel(models.Model):
template = models.ForeignKey(Template)
...
When I switch the template field on MyModel to another template via the admin, MyModel is not getting the correct template for display. I get "TemplateDoesNotExist" sometimes. Other times it continues to display using the previous template. It never updates to the new template though. I have to use the admin action "repopulate cache with selected items" to get it to find the template again.
I am using a class based view and defining "get_template_names()" to get the Template model from the foreignkey.
Shouldn't the template be updating when the foreignkey is changed? And shouldn't it find the correct one either in the cache or via the database?
(Django 1.5)
No, it should not update the cache if your ForeignKey is changed. To quote the docs: "dbtemplates uses Django’s default caching infrastructure for caching, and operates automatically when creating, updating or deleting templates in the database". That applies to changing the templates, but not to setting relations to a template.
As to what you're trying to accomplish, I'm out of ideas. If you're simply changing the relation to a template, and use the template's name field in your get_template_names view method, I don't see how the cache needs to be invalidated at all. Each template has separate names after all, so the template loader will now what to do when it tries to load the template form the database.
Good point about caching and ForeignKey relations. Maybe the issue is not related to caching? As you describe, I am returning the template's '
So here's the sequence I just went though.
- No template set on model (ie foreignkey is empty) -> loads default template in browser, no problem
- Using admin, update template field on model, set to existing Template object in database ("foo/sometemplate.html"), save model. Still loads default template in browser (not sure why this is - I think this problem is in my code though).
- Restart server process
- Try to load page, error TemplateDoesNotExist (correctly returns the name of the Template object I chose "foo/sometemplate.html", however).
Debug Template-loader postmortem says: Django tried loading these templates, in this order:
* Using loader dbtemplates.loader.Loader:
* Using loader django.template.loaders.filesystem.Loader:
* Using loader django.template.loaders.app_directories.Loader:
* places
* places
* etc
- Go to admin page, run "repopulate cache with selected templates"
- page loads with correct template from dbtemplates
Further info:
Forgetting about the part with changing foreignkeys on my models...
If I leave the foreign key alone, and have no cache settings in my settings.py, I will get TemplateDoesNotExist when I try to load a page with that template. Then I go to the admin, "Repopulate cache with selected template", and the page loads ok. Wait a few minutes, and then try to load page again. I get TemplateDoesNotExist again.
So without cache settings created in settings.py, I'm not sure where the "Repopulate cache with selected template" is loading the template to. But it seems that the Loader is not correctly falling back to getting the template from the database if it fails to find it in the cache.
Hope that helps pinpoint the issue.