Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:22:58+00:00 2026-05-10T19:22:58+00:00

In Django, given excerpts from an application animals likeso: A animals/models.py with: from django.db

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:22:59+00:00Added an answer on May 10, 2026 at 7:22 pm

    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:

    from django.contrib.contenttypes.models import ContentType from django.views.generic import create_update  def update_object_as_child(parent_model_class):    '''    Given a base models.Model class, decorate a function to return      create_update.update_object, on the child class.     e.g.    @update_object(Animal)    def update_object(request, object_id):       pass    kwargs should have an object_id defined.   '''    def decorator(function):       def wrapper(request, **kwargs):           # may raise KeyError           id = kwargs['object_id']            parent_obj = parent_model_class.objects.get( pk=id )            # following http://www.djangosnippets.org/snippets/1031/           child_class = parent_obj.content_type.model_class()            kwargs['model'] = child_class            # rely on the generic code for testing/validation/404           return create_update.update_object(request, **kwargs)       return wrapper    return decorator 

    And in animals/views.py, I have:

    from mysite.core.views.create_update import update_object_as_child  @update_object_as_child(Animal) def edit_animal(request, object_id):   pass 

    And in animals/urls.py, I have:

    urlpatterns += patterns('animals.views',   url(r'^edit/(?P<object_id>\d+)$', 'edit_animal', name='edit_animal'), ) 

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
Given: from django.db import models class MyModel(models.Model): ... owners = models.CharField(max_length=255, blank=False) where owners
I'm struggling with the design of a django application. Given the following models: class
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass
I'm a Django newbie who needs your help Have this database model: class Record(models.Model):
I'm from a PHP background but have been given a Django project to administer,
given a string identifying a Django model I have to obtain the associated object
Given a situation in Django 1.0 where you have extra data on a Many-to-Many
I'm plugging Django into an existing system. I have been given a legacy schema,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.