django-multiselectfield/example/app/models.py

76 lines
2.5 KiB
Python
Raw Normal View History

2013-11-19 18:32:11 +00:00
# -*- coding: utf-8 -*-
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com>
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
from django.db import models
2016-09-22 20:12:56 +00:00
from django.utils.translation import gettext as _
2013-11-19 18:32:11 +00:00
from multiselectfield import MultiSelectField
CATEGORY_CHOICES = (
2016-09-23 11:55:08 +00:00
(1, _('Handbooks and manuals by discipline')),
(2, _('Business books')),
(3, _('Books of literary criticism')),
(4, _('Books about literary theory')),
(5, _('Books about literature')),
2013-11-19 18:32:11 +00:00
)
TAGS_CHOICES = (
('sex', _('Sex')), # noqa: E241
('work', _('Work')), # noqa: E241
('happy', _('Happy')), # noqa: E241
('food', _('Food')), # noqa: E241
('field', _('Field')), # noqa: E241
('boring', _('Boring')), # noqa: E241
('interesting', _('Interesting')), # noqa: E241
('huge', _('Huge')), # noqa: E241
('nice', _('Nice')), # noqa: E241
2016-09-22 20:12:56 +00:00
)
PROVINCES = (
('AB', _("Alberta")),
('BC', _("British Columbia")),
)
STATES = (
('AK', _("Alaska")),
('AL', _("Alabama")),
('AZ', _("Arizona")),
)
PROVINCES_AND_STATES = (
(_("Canada - Provinces"), PROVINCES),
(_("USA - States"), STATES), # noqa: E241
2013-11-19 18:32:11 +00:00
)
class Book(models.Model):
title = models.CharField(max_length=200)
categories = MultiSelectField(choices=CATEGORY_CHOICES,
2013-11-30 18:25:11 +00:00
max_choices=3,
# default='1,5')
2013-11-30 18:25:11 +00:00
default=1)
tags = MultiSelectField(choices=TAGS_CHOICES,
null=True, blank=True)
2016-09-23 11:55:08 +00:00
published_in = MultiSelectField(_("Province or State"),
choices=PROVINCES_AND_STATES,
max_choices=2)
2013-11-19 18:32:11 +00:00
def __str__(self):
return self.title
def __unicode__(self):
return self.__str__()