webdevRefinery Forum: First time using Django.... - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 20 May 2011 - 12:42 PM (#1)

First time using Django....


So, I thought I'd better bite the proverbial bullet and actually make a project with Django. It's the predominant Python web framework, so its nice to have experience of it and stuff.

I've come across a problem. I'm making a content feed, kinda like the one you'd find on Facebook. I plan for it to have statuses, blogs, links, photos, videos and geographic check-ins. Each of these content types will have there own page to display data. For example, the blog may put the title, and first x amount of characters in the feed and when you click the title it'll take you to the full blog post. A status would just have a page with... well, the status. A video item will probably display it in a window bigger than a tiny preview... you get the idea anyway. Each one will act differently, and have distinct behaviour.

There is also a degree of generic attributes which each item has, regardless of its specialised type. Each post will require the date and a boolean value to decide whether it is shown or not.

Because of the way a feed works, I need all the content together in one table which will allow me to simply get the top last posted n number of elements, and output them on the feed. The way I've done this is have a generic FeedItem class, which has an ID as a primary key (serving as a GUID for each content type and allowing me to query the top n elements easily), the date, whether the item is shown, and a field which indicates what type it is. Each type is then an extension of this generic feed item, which implements its own specific logic.

In my application, I want to be able to fetch the top n generic items, and be able to treat them like they're the child classes. So instead of getting back the top 10 feeditems, for example, I want those 10 to be like, 5 statuses, 3 blogs and 2 links or whatever.

The way I've done this so far looks like this.

from django.db import models
import datetime

VALID_TYPES = ((1, 'Status'), (2, 'Blog'), (3, 'Photo'), (4, 'Link'), (5, 'CheckIn'), (6, 'Video'))
types_dict = {1:'Status', 2:'Blog', 3:'Photo', 4:'Link', 5:'CheckIn', 6:'Video'}

class FeedItem(models.Model):
    id = models.IntegerField(primary_key=True)
    date = models.DateTimeField(default = datetime.datetime.now())
    liked = models.IntegerField(default = 0)
    type = models.IntegerField(choices = VALID_TYPES)
    show = models.BooleanField(default=True)
    
    def getChild(self):
        print(dir(self))

class Status(FeedItem):
    
    class Meta:
        verbose_name_plural = "Statuses"
    
    genericItem = models.ForeignKey(FeedItem)
    text = models.CharField(max_length=160)
    
    def save(self, *args, **kwargs):
        self.type = 1
        super(Status, self).save(*args, **kwargs)

class Blog(FeedItem):
    content = models.TextField()
    title = models.CharField(unique=True, max_length=160)
    
    def __unicode__(self):
        return self.title
    
    def is_visible(self):
        return self.show
    is_visible.boolean = True
    
    def save(self, *args, **kwargs):
        self.type = 2
        super(Blog, self).save(*args, **kwargs)
        
class Photo(FeedItem):
    filename = models.CharField(unique=True, max_length=32)
    title = models.CharField(unique=True, max_length=160)

    def save(self, *args, **kwargs):
        self.type = 3
        super(Photo, self).save(*args, **kwargs)

class Link(FeedItem):
    location = models.CharField(max_length=2000)
    title = models.CharField(max_length=160)

    def save(self, *args, **kwargs):
        self.type = 4
        super(Link, self).save(*args, **kwargs)
        
class CheckIn(FeedItem):
    location = models.CharField(max_length=200)
    title = models.CharField(max_length=160)
    
    def save(self, *args, **kwargs):
        self.type = 5
        super(CheckIn, self).save(*args, **kwargs)
    
class Video(FeedItem):
    embedlink = models.CharField(max_length=2000)
    
    def save(self, *args, **kwargs):
        self.type = 6
        super(Video, self).save(*args, **kwargs)


It isn't finished yet, in terms of fleshing out some of the meta stuff and functionality on some of the other content types.

My plan for getting the child element would be something along the lines of

#in the FeedItem class
def getChild(self):
  child = getattr(self, types_dict[self.type].lower())
  return child

Either that, or
def getChild(self):
  return self.__dict__[types_dict[self.type].lower()]


Then possibly overwriting the return method of get for FeedItem to return the child elements. Not too sure yet though.

The problem with this is that it all seems awfully messy. Is there a cleaner way of doing it that I'm obviously missing?

Also, is there a way of disabling the primary keys for the child classes? They'll already have GUIDs from their entry in the generic table, so generating another autoincremented ID really isn't required.
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services