Basic gallery model for django

Django -- Posted on Nov. 26, 2017

This code appears to be a simplified Django model implementation for a Gallery and Image management system. Let's go through the code and understand its structure and purpose:

  1. Importing Libraries: The code starts by importing the necessary modules:

    • os: This module provides a portable way of using operating system-dependent functionality.
    • datetime: This module supplies classes for manipulating dates and times.
    • models from django.db: This module contains various model-related classes for Django's database ORM (Object-Relational Mapping).
    • ugettext as _ from django.utils.translation: This is used for translating text strings in Django applications.
  2. Timestamped Abstract Model: The Timestamped class is an abstract model that contains two fields:

    • created_at: A DateTimeField that stores the timestamp when the object is created. The auto_now_add=True parameter ensures that this field is automatically set to the current datetime when an object is created.
    • updated_at: A DateTimeField that stores the timestamp when the object is updated. The auto_now=True parameter ensures that this field is automatically updated to the current datetime whenever the object is saved.

    The abstract = True in the Meta class indicates that this model is an abstract base class and won't create a database table on its own.

  3. Published Abstract Model: The Published class is another abstract model that contains a single field:

    • is_published: A BooleanField that indicates whether an object is published (True) or not published (False) by default.

    Similar to Timestamped, this class is also an abstract base class (abstract = True).

  4. Gallery Model: The Gallery model inherits from Timestamped and Published. It represents a gallery of images and has the following fields:

    • name: A CharField with a maximum length of 100 characters, representing the name of the gallery.

    The default_related_name is set to 'galleries', which allows you to use reverse relations from related models. The verbose_name and verbose_name_plural provide human-readable names for the model in the Django admin interface.

    The __str__ method returns the name of the gallery as its string representation.

  5. get_upload_path Function: This function is defined to customize the file upload path for the Image model. It takes an instance of the Image model and the filename and generates a path based on the gallery's name and the current date (day, month, year).

  6. Image Model: The Image model inherits from Timestamped and Published. It represents an image within a specific gallery and has the following fields:

    • gallery: A foreign key to the Gallery model, creating a many-to-one relationship between images and galleries.
    • image: An ImageField that stores the image file and uses the get_upload_path function to determine the file upload path.

    The default_related_name, verbose_name, and verbose_name_plural attributes are set similar to the Gallery model.

    The __str__ method returns the name of the image file as its string representation.

Overall, this Django model implementation allows you to create galleries with associated images, keep track of creation and update timestamps, manage publication status, and customize the image upload path based on the gallery's name and the current date.

 

              
                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
                  
   
            

Related Posts