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 6386313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:00:35+00:00 2026-05-25T03:00:35+00:00

the situation In my example I want to create a Page model with a

  • 0

the situation

In my example I want to create a Page model with a many to many relationship with a content-blocks model.

  1. A page has a title, slug, and main content block.
  2. content blocks have a title and a content block.

What I can get:

Showing page.blocks in the admin form displays a multi select of content blocks

Creating an inline form for the content blocks on the page admin shows several selects with a + sign to add more

What I am trying to accomplish:

Full CRUD on content block on the page admin

Note: Due to the difficulty of my request, I’m beginning to believe the UX pattern im trying to accomplish is wrong. If I want a content creator to come in and create a page, pick some existing content blocks (ex: an existing sidebar content block), and then create a new custom block. I don’t think i want him to have to jump all over the place to do this…

Related Question without solutions:
How do I use a TabularInline with editable fields on a ManyToMany relationship?

EDIT

my admin.py

from django.contrib import admin
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from my_flatpages.models import ExtendedFlatPage, ContentBlock
from mptt.admin import MPTTModelAdmin
from django import forms
import settings

"""
Extended Flatpage Form
"""
class ExtendedFlatPageForm(FlatpageForm):
    class Meta:
        model = ExtendedFlatPage


"""
Page Content Block inline form
"""
class ContentBlockInlineAdminForm(forms.ModelForm):
    # Add form field for selecting an existing content block
    content_block_choices = [('', 'New...')]
    content_block_choices.extend([(c.id, c) for c in ContentBlock.objects.all()])
    content_blocks = forms.ChoiceField(choices=content_block_choices, label='Content Block')

    def __init(self, *args, **kwargs):
        super(ContentBlockInlineAdminForm, self).__init__(*args, **kwargs)

        # Show as existing content block if it already exists
        if self.instance.pk:
            self.fields['content_block'].initial = self.instance.pk
            self.fields['title'].initial = ''
            self.fields['content'].initial = ''

        # Make title and content not required so user can opt to select existing content block
        self.fields['title'].required = False
        self.fields['content'].required = False

    def clean(self):
        content_block = self.cleaned_data.get('content_block')
        title = self.cleaned_data.get('title')
        content = self.cleaned_data.get('content')

        # Validate that either user has selected existing content block or entered info for new content block
        if not content_block and not title and not content:
            raise forms.ValidationError('You must either select an existing content block or enter the title and content for a new content block')


"""
Content Block Inline Admin
"""
class ContentBlockInlineAdmin(admin.TabularInline):
    form = ContentBlockInlineAdminForm

    class Meta:
        model = ContentBlock
        extra = 1


"""
Extended Flatpage Admin
"""
class ExtendedFlatPageAdmin(FlatPageAdmin, MPTTModelAdmin):
    form = ExtendedFlatPageForm
    fieldsets = (
        (
            None, 
            {
                'fields': ('url', 'title', 'content', ('parent', 'sites'))
            }
        ),
        (
            'SEO Fields', 
            {
                'fields': ('seo_title', 'seo_keywords', 'seo_description'), 
            'classes': ('collapse', )
            }
        ),
        (
            'Advanced options', 
            {
                'fields': ('enable_comments', 'registration_required', 'template_name'), 
            'classes': ('collapse', )
            }
        ),
    )
    inlines = (ContentBlockInlineAdmin,)


    class Media:
        js = (
            'https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js',
            settings.MEDIA_URL + 'js/tinymce/jquery.tinymce.js',
            settings.MEDIA_URL + 'js/init_tinymce.js'
        )

admin.site.unregister(FlatPage)
admin.site.register(ExtendedFlatPage, ExtendedFlatPageAdmin)

  • 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-05-25T03:00:35+00:00Added an answer on May 25, 2026 at 3:00 am

    This isn’t the answer I was looking for, BUT, What I ended up going with is

    class Page(models.Model):
        ....
    
    class ContentBlock(models.Model):
        page = models.ForeignKey(
            Page, 
            blank = True,
            null = True,
        )
        ....
    

    and then having a regular tabular inline for ContentBlock on the page admin form.

    So that way I can have page specific content blocks related to a page, AND be able to have generic content blocks able to be used wherever.

    Then, I created an inclusion tag to render a content block by name that I use in my templates.

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

Sidebar

Related Questions

Simple situation... With any random XML file, I want to create a list of
Situation: I'm trying run an https store (xcart) under one domain secure.example.com and I
For a situation like capturing text incrementally, for example if you were receiving all
This is often situation, but here is latest example: Companies have various contact data
Situation: you've got a .swf embedded in an html page, and when you click
Situation: Site with content protected by username/password (not all controlled since they can be
Situation: Table TBL has ~10k entries for deletion, Table TBL has 14 child tables
I have a situation where I want to assemble a Doctrine select query based
Consider the following situation: I want to replace links in a string, specifically I
I have situation. I have to create a Sports Club system in JAVA. There

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.