Blog entries on June 2006

  • Django Admin interface for a Blog in 5 mins

    By Nuno Mariz, on 13 June 2006 @ 21:16
    It's so cool with Django:
    from django.db import models
    from django.contrib.auth.models import User
    
    class Tag(models.Model):
        name = models.CharField(maxlength=200, core=True)
     
        class Admin:
            ordering = ['name']   
            
        def __str__(self):
            return self.name
    
    PUBLICATION_CHOICES = (
        ('Draft', 'Draft'),
        ('Published', 'Published'),
    )
            
    class Post(models.Model):
        author = models.ForeignKey(User)
        title = models.CharField(maxlength=200)    
        summary = models.TextField()
        body = models.TextField()
        created = models.DateTimeField(default=models.LazyDate())
        last_modified = models.DateTimeField(auto_now=True)
        enable_comments = models.BooleanField(default=True)
        tags = models.ManyToManyField(Tag)
        publication = models.CharField(maxlength=32, choices=PUBLICATION_CHOICES, radio_admin=True, default='Published')
    
        class Admin:
            ordering = ['-created']
            search_fields = ['title']
            list_display = ('title','author', 'created')
            list_filter = ('created','last_modified','enable_comments','publication', 'tags')
    
        def __str__(self):
            return self.title
        
    
    class Comment(models.Model):
        post = models.ForeignKey(Post)
        name = models.CharField(maxlength=100)
        email = models.EmailField()
        website = models.CharField(maxlength=200, blank=True, null=True)
        comment = models.TextField()
        created = models.DateTimeField(auto_now_add=True)
        last_modified = models.DateTimeField(auto_now=True)
        
        class Admin:
            ordering = ['-created']
            search_fields = ['name']
            list_display = ('post','name', 'created')
            list_filter = ('created','last_modified')
    
        def __str__(self):
            return self.name