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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:17:22+00:00 2026-05-13T08:17:22+00:00

Say, there is a Page that has many blocks associated with it. And each

  • 0

Say, there is a Page that has many blocks associated with it. And each block needs custom rendering, saving and data.

Simplest it is, from the code point of view, to define different classes (hence, models) for each of these models. Simplified as follows:

class Page(models.Model):
    name = models.CharField(max_length=64)

class Block(models.Model):
    page = models.ForeignKey(Page)

    class Meta():
        abstract = True

class BlockType1(Block):

    other_data = models.CharField(max_length=32)

    def render(self):
        """Some "stuff" here """
        pass

class BlockType2(Block):

    other_data2 = models.CharField(max_length=32)

    def render(self):
        """Some "other stuff" here """
        pass

But then,

  • Even with this code, I can’t do a query like page.block_set.all() to obtain all the different blocks, irrespective of the block type.
  • The reason for the above is that, each model defines a different table; Working around to accomplish it using a linking model and generic foreign keys, can solve the problem, but it still leaves multiple database tables queries per page.

What would be the right way to model it? Can the generic foreign keys (or something else) be used in some way, to store the data preferably in the same database table, yet achieve inheritance paradigms.

Update:

My point was, How can I still get the OOP paradigms to work. Using a same method with so many ifs is not what I wanted to do.

The best solution, seems to me, is to create separate standard python class (Preferably in a different blocks.py), that defines a save which saves the data and its “type” by instantiating the same model. Then create a template tag and a filter that calls the render, save, and other methods based on the model’s type.

  • 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-13T08:17:23+00:00Added an answer on May 13, 2026 at 8:17 am

    Don’t model the page in the database. Pages are a presentation thing.

    First — and foremost — get the data right.

    “And each block needs custom rendering, saving and data.” Break this down: you have unique data. Ignore the “block” and “rendering” from a model perspective. Just define the data without regard to presentation.

    Seriously. Just define the data in the model without any consideration of presentation or rending or anything else. Get the data model right.

    If you confuse the model and the presentation, you’ll never get anything to work well. And if you do get it to work, you’ll never be able to extend or reuse it.

    Second — only after the data model is right — you can turn to presentation.

    Your “blocks” may be done simply with HTML <div> tags and a style sheet. Try that first.
    After all, the model works and is very simple. This is just HTML and CSS, separate from the model.

    Your “blocks” may require custom template tags to create more complex, conditional HTML. Try that second.

    Your “blocks” may — in an extreme case — be so complex that you have to write a specialized view function to transform several objects into HTML. This is very, very rare. You should not do this until you are sure that you can’t do this with template tags.


    Edit.

    “query different external data sources”

    “separate simple classes (not Models) that have a save method, that write to the same database table.”

    You have three completely different, unrelated, separate things.

    • Model. The persistent model. With the save() method. These do very, very little.
      They have attributes and a few methods. No “query different external data sources”. No “rendering in HTML”.

    • External Data Sources. These are ordinary Python classes that acquire data.
      These objects (1) get external data and (2) create Model objects. And nothing else. No “persistence”. No “rendering in HTML”.

    • Presentation. These are ordinary Django templates that present the Model objects. No external query. No persistence.

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

Sidebar

Related Questions

Let's say there is a page with 100 different user photo's shown on the
Is there a way to have lightbox on page load?? Say like when the
Say there is a dict variable that grows very large during runtime - up
I have a web-site that generates some simple tabular data as html tables, many
On many sites now, say you have a toolbar/table-header that is midway in the
Let's say I have an ActiveRecord model called Book that has a has_many association
Let's say I have this setup: An Area has many :places The routes.rb is
I have many to one relation, let's say User has a Company. Company entity
Experts, I'm creating a glossary that has each letter of the alphabet as a
I am writing a web application that has a page that returns a list

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.