Guest User

Untitled

a guest
Dec 23rd, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. class Post(models.Model):
  2.     title = models.CharField(max_length=200)
  3.     slug = models.CharField(max_length=200)
  4.  
  5.     source_url = models.URLField()
  6.  
  7.     needs_update = models.BooleanField(default=True)
  8.  
  9.     created = models.DateTimeField(auto_now_add=True)
  10.     updated = models.DateTimeField(auto_now=True)
  11.  
  12.  
  13. #models.py
  14. class ItemBase(models.Model):
  15.     post = models.ManyToManyField(Post, related_name='%(class)s_related')
  16.     title = models.CharField(max_length=250)
  17.     created = models.DateTimeField(auto_now_add=True)
  18.     updated = models.DateTimeField(auto_now=True)
  19.  
  20.     class Meta:
  21.         abstract = True
  22.  
  23.     def __str__(self):
  24.         return self.title
  25.  
  26.     def render(self):
  27.         return render_to_string('content/items/{}.html'.format(self._meta.model_name), {'item': self})
  28.  
  29.  
  30. class Text(ItemBase):
  31.     content = models.TextField()
  32.  
  33.  
  34. class File(ItemBase):
  35.     file = models.FileField(upload_to='files', blank=True, null=True)
  36.  
  37.  
  38. class Video(ItemBase):
  39.     url = models.URLField()
  40.  
  41.  
  42.  
  43. class Image(ItemBase):
  44.     image_url = models.URLField(blank=True, null=True)
  45.     image_file = models.ImageField(upload_to='posts/%Y/%m/%d',
  46.                                    blank=True, null=True)
  47.  
  48. #admin.py
  49. from django.contrib import admin
  50. from .models import Post
  51.  
  52.  
  53.  
  54. @admin.register(Post)
  55. class PostAdmin(admin.ModelAdmin):
  56.     list_display = ['title', 'needs_update', 'created']
  57.     list_filter = ['created', 'needs_update']
  58.     fields = ('title', 'image_related')
Advertisement
Add Comment
Please, Sign In to add comment