开发者

Generalized Model in Django

开发者 https://www.devze.com 2023-03-07 22:45 出处:网络
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:

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 开发者_StackOverflow= 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号