Django Forms: The Most DRY Way to Organize Create / Update Forms for Inherited Models

I have two inherited models with multiple tables:

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, the form class for BlogPost also inherits from the PostForm class:

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

    def clean(self):
        ...

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

I used processing / updating for both models in two non-class views. To make things dry, I decided to try class-based Django. But due to the lack of examples and the unfriendliness of users of related documents and various approaches, I am confused.

The idea is to have class-based form submissions for the model Postand inherit them for BlogPost. How should I adapt presentation classes?

PostCreate PostUpdate? , .

FormView / ? , , .

( /), mixin ?

+5
1

, . , , .

:

  • .
  • , .

. , .

. :

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

, , :

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

, ( type ). , , , .

+8

All Articles