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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:07:47+00:00 2026-05-22T16:07:47+00:00

My first question helped me a LOT, so I figured I’d go ahead and

  • 0

My first question helped me a LOT, so I figured I’d go ahead and ask a second one.

My current (practice) project is to create a generalized model. Here’s what I’ve written so far:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Block(models.Model):
    title = models.CharField(man_length=50)
    #Make sure child classes have content defined!
    #content = models.BooleanField(default=False)
    owner = models.ForeignKey('User')
    access = models.ManytoManyField('User', null=True)
    links = models.ManytoManyField('self', null=True)

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.title


class Text(Block)
    content = models.TextField(max_length=100)

class URL(Block)
    content = models.URLField()

class Email(Block)
    content = models.EmailField()

Please note I haven’t actually tested this yet – I don’t think my logic on this so far is going to work.

A few goals:

1) owner there points to the creator of the block. I think that should work.

2) access should point to Users (other than the owner) who can edit the block. I think this should work too. (with proper views of course)

3) links should allow linking between blocks – so I can retrieve a block and any related blocks (and so on up if need be)

4) content should of course be the content of the block(I have the three sample simple data types here), which ideally can be any number of things (implemented here via the abstract base class).

5) For the classes Email, URL, and Text, I’d like to be able to write a (generalized) view which from one url input, returns an appropriate block of any of the three types. I think this may require an autoincrementing field which guarantees a unique value between the three models. Honestly, I’m not sure how to do this at all.

6) Of course ideally this should be readily extensible if at all possible.

I’m of course very unsure if this is the best way to go about achieving what I’m trying to do. Any suggestions, help, tips, tricks, or bad jokes accepted!

Thank you very much for your help!

EDIT: relating to number 5, I have a rough view (with a syntax error) that I think might do the trick:

def getblock(request, block, no):
    data = get_object_or_404(%s,%s) % (block,no)
    return render_to_response('single.html',{'data':data})

Thoughts?

  • 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-22T16:07:48+00:00Added an answer on May 22, 2026 at 4:07 pm

    Your code seems correct to me. I also worked on a generic class for my project and it was similar to this one. One remark, when you get a block, you should do some type checking to retrieve the class data so, I recommend you to add a field to store its type. e.g:

    class Block(models.Model):
      title = models.CharField(max_length=50)
      type = models.IntegerField()# 1 for text, 2 for email and 3 for url
      #Make sure child classes have content defined!
      #content = models.BooleanField(default=False)
      owner = models.ForeignKey('User')
      access = models.ManytoManyField('User', null=True)
      links = models.ManytoManyField('self', null=True)
    

    Then in your view:

    def getblock(request, block, no):
      if block.type == 1:
        data = get_object_or_404(Block,id=no).text
      elif block.type == 2:
        data = get_object_or_404(Block,id=no).email
      elif block.type == 3:
        data = get_object_or_404(Block,id=no).url
      return render_to_response('single.html',{'data':data})
    

    You can access the subclasses with lower case representation of that child class. I think that will work nicely. Also I don’t get the dictionary type arguments you wrote and that gave a syntax error when I executed. Anyway, I also corrected some of the syntax errors, too.

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

Sidebar

Related Questions

Hey there guys, this is my first question on Stack Overflow. I figured this
this is my first ever question on this website but it has helped me
One day on from my first question, I'm afraid to say I'm back. A
My first question on this topic was answered and it helped me move forward
It is my first question. I personally find this website awesome and helped me
First question on Stackoverflow (.Net 2.0): So I am trying to return an XML
First question here: it is a very short yet fundamental thing in Java that
First question from me; I'm currently fixing a graphing service that uses XSLFO to
First question is, given a url to an mp4 video, how can I save
First question here so hello everyone. The requirement I'm working on is a small

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.