In Django, given excerpts from an application animals likeso:
A animals/models.py with:
from django.db import models from django.contrib.contenttypes.models import ContentType class Animal(models.Model): content_type = models.ForeignKey(ContentType,editable=False,null=True) name = models.CharField() class Dog(Animal): is_lucky = models.BooleanField() class Cat(Animal): lives_left = models.IntegerField()
And an animals/urls.py:
from django.conf.urls.default import * from animals.models import Animal, Dog, Cat dict = { 'model' : Animal } urlpatterns = ( url(r'^edit/(?P<object_id>\d+)$', 'create_update.update_object', dict), )
How can one use generic views to edit Dog and/or Cat using the same form?
I.e. The form object that is passed to animals/animal_form.html will be Animal, and thus won’t contain any of the specifics for the derived classes Dog and Cat. How could I have Django automatically pass a form for the child class to animal/animals_form.html?
Incidentally, I’m using Djangosnippets #1031 for ContentType management, so Animal would have a method named as_leaf_class that returns the derived class.
Clearly, one could create forms for each derived class, but that’s quite a lot of unnecessary duplication (as the templates will all be generic — essentially {{ form.as_p }}).
Incidentally, it’s best to assume that Animal will probably be one of several unrelated base classes with the same problem, so an ideal solution would be generic.
Thank you in advance for the help.
Alright, here’s what I’ve done, and it seems to work and be a sensible design (though I stand to be corrected!).
In a core library (e.g. mysite.core.views.create_update), I’ve written a decorator:
And in animals/views.py, I have:
And in animals/urls.py, I have:
Now I only need a unique edit function for each base class, which is trivial to create with a decorator.
Hope someone finds that helpful, and I’d be delighted to have feedback.