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

  • SEARCH
  • Home
  • 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 7851621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:13:34+00:00 2026-06-02T19:13:34+00:00

I have 2 multi-table inherited models like these: class Post(models.Model): title = models.CharField(max_length=100, blank=True,

  • 0

I have 2 multi-table inherited models like these:

class Post(models.Model):
    title = models.CharField(max_length=100, blank=True, null=True)
    text = models.TextField(blank=True, null=True)
    location = models.PointField()
    ...

class BlogPost(Post):
    blog = models.ForeignKey(Blog)

Similarly, form class for BlogPost also inherits from PostForm class:

class PostForm(MapModelForm):
    ...
    class Meta:
        model = Post

    def clean(self):
        ...

class BlogPostForm(PostForm):
    class Meta:
        model = BlogPost

I used to handle create / update for both models in 2 non-class-based views. To make things DRY, I decided to give Django’s class-based generic views a try. But due to the lack of examples and user unfriendliness of the related documents and variety of approaches, I am confused.

The idea is to have class based form views for the Post model and inherit them for the BlogPost. How should I tailor the view classes?

Should I have separate PostCreate and PostUpdate views? Then the two classes are almost identical and not DRY at all.

Should I have a single FormView for both update / create of a post? It works for creation but I couldn’t figure out how to do this for for update.

Should I build custom view (or two views for create / update) using provided mixin classes instead of inheriting from the views directly?

  • 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. Editorial Team
    Editorial Team
    2026-06-02T19:13:35+00:00Added an answer on June 2, 2026 at 7:13 pm

    You’re right that the class-based view documentation is rather primitive. No doubt it will improve, but for the moment you need to be prepared to experiment and to read the source.

    You are trying to do two things:

    1. Find a good way to organize your views using classes.
    2. Refactor your code to avoid repetition.

    It’s important to tackle these one at a time. Figure out the right class hierarchy first, and only then figure out how to factor out the repetition.

    Let’s have a go. Your class hierarchy is going to look something like this:

    from django.views import generic
    
    class PostCreateView(generic.CreateView):
        form_class = PostForm
        model = Post
    
    class PostUpdateView(generic.UpdateView):
        form_class = PostForm
        model = Post
    
    class BlogPostCreateView(generic.CreateView):
        form_class = BlogPostForm
        model = BlogPost
    
    class BlogPostUpdateView(generic.UpdateView):
        form_class = BlogPostForm
        model = BlogPost
    

    This involves a lot of repetition, but it’s clear now how to factor out some of it:

    from django.views import generic
    
    class PostView(generic.FormView):
        form_class = PostForm
        model = Post
    
    class PostCreateView(PostView, generic.CreateView): pass
    class PostUpdateView(PostView, generic.UpdateView): pass
    
    class BlogPostView(PostView):
        form_class = BlogPostForm
        model = BlogPost
    
    class BlogPostCreateView(BlogPostView, generic.CreateView): pass
    class BlogPostUpdateView(BlogPostView, generic.UpdateView): pass
    

    And if you’re still unhappy with the amount of repetition here, you can further automate the construction of these classes (using type to create classes at runtime). That’s probably a step too far, though, until you’re completely comfortable with the use of class-based views.

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

Sidebar

Related Questions

I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have a multi-row insert that looks something like: insert into table VALUES (1,
I have a multi-table SELECT query which compares column values with itself like below:
I have a multi-table query, similar to this (simplified version) SELECT columns, count(table2.rev_id) As
I have created a multi column datastore on a table that allows me to
I have a large table with a multi-part index. I need to run several
We have a table like this: id mid mult tr result ---------------------------- 1 1
This is a multi-part question. I have a table similar to: CREATE TABLE sales_data
I have a multi-table SQL query. My need is: The query should I generate
I have an ASP.NET 3.5 webforms project written in VB that involves a multi-table

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.