Dimitris Kougioumtzis Blog About technology

Basic gallery model for django

Category Django

Posted on Nov. 26, 2017



Basic gallery model for django


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import datetime
from django.db import models
from django.utils.translation import ugettext as _


class Timestamped(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class Published(models.Model):
    is_published = models.BooleanField(default=False)

    class Meta:
        abstract = True


class Gallery(Timestamped, Published):
    name = models.CharField(max_length=100)

    class Meta:
        default_related_name = 'galleries'
        verbose_name = _('gallery')
        verbose_name_plural = _('galleries')

    def __str__(self):
        return self.name


def get_upload_path(instance, filename):
    return os.path.join('{}/{}/{}/{}/{}'.format(
        instance.gallery.name, datetime.datetime.now().day,
        datetime.datetime.now().month,
        datetime.datetime.now().year, filename))


class Image(Timestamped, Published):
    gallery = models.ForeignKey(Gallery)
    image = models.ImageField(upload_to=get_upload_path)

    class Meta:
        default_related_name = 'images'
        verbose_name = _('image')
        verbose_name_plural = _('images')

    def __str__(self):
        return self.image.name

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere