Git Product home page Git Product logo

django-modeltools's Introduction

Installation

  1. pip install -e git+git://github.com/hzdg/django-modeltools.git#egg=django-modeltools

Included Tools

Enum

The Enum class lets you quickly define enumeration types for model field values. The constructor accepts kwargs where the key is the name of the "constant" and the value is a two-tuple containing the value of the constant (which will be stored in the database) and a label.

Usage

In models.py:

from modeltools import Enum

class MyModel(models.Model):

    Color = Enum(
        RED=('r', 'Red'),
        GREEN=('g', 'Green'),
        BLUE=('b', 'Blue'),
    )

    color = models.CharField(max_length=1, choices=Color.choices())

Elsewhere:

m = MyModel.objects.filter(color=MyModel.Color.RED)

format_filename

The format_filename function provides an easy way to name user media (uploaded files) based on properties of the model that stores them.

Usage

In models.py:

from modeltools import format_filename as _ff

class Person(models.Model):
	first_name = models.CharField(max_length=50)
	last_name = models.CharField(max_length=50)
	middle_name = models.CharField(max_length=50)
	avatar = models.ImageField(upload_to=_ff('avatars/{last_name}_{first_name}'))

In the above example, {first_name} and {last_name} will be replaced with the corresponding values from the Person instance. The uploaded file will automatically retain its original extension.

By default, the properties used in the formatting pattern will be converted to lowercase, stripped of non-word characters, and have their spaces replaced with underscores. (This behavior can be changed by providing extra arguments to the format_filename function.) The rest of the formatting string will be unaffected.

django-modeltools's People

Contributors

lettertwo avatar matthewwithanm avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.