mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
Assert that StatusField respects default arguments.
This commit is contained in:
parent
86400554d4
commit
cce804da4e
3 changed files with 25 additions and 3 deletions
|
|
@ -60,7 +60,8 @@ class StatusField(models.CharField):
|
|||
"To use StatusField, the model '%s' must have a STATUS choices class attribute." \
|
||||
% cls.__name__
|
||||
setattr(self, '_choices', cls.STATUS)
|
||||
setattr(self, 'default', tuple(cls.STATUS)[0][0]) # sets first as default
|
||||
if not self.has_default():
|
||||
setattr(self, 'default', tuple(cls.STATUS)[0][0]) # sets first as default
|
||||
super(StatusField, self).contribute_to_class(cls, name)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _
|
|||
from model_utils.models import TimeStampedModel, StatusModel, TimeFramedModel
|
||||
from model_utils.tracker import ModelTracker
|
||||
from model_utils.managers import QueryManager, InheritanceManager, PassThroughManager
|
||||
from model_utils.fields import SplitField, MonitorField
|
||||
from model_utils.fields import SplitField, MonitorField, StatusField
|
||||
from model_utils import Choices
|
||||
|
||||
|
||||
|
|
@ -241,3 +241,13 @@ class TrackedMultiple(models.Model):
|
|||
|
||||
name_tracker = ModelTracker(fields=['name'])
|
||||
number_tracker = ModelTracker(fields=['number'])
|
||||
|
||||
|
||||
class StatusFieldDefaultFilled(models.Model):
|
||||
STATUS = Choices((0, "no", "No"), (1, "yes", "Yes"))
|
||||
status = StatusField(default=STATUS.yes)
|
||||
|
||||
|
||||
class StatusFieldDefaultNotFilled(models.Model):
|
||||
STATUS = Choices((0, "no", "No"), (1, "yes", "Yes"))
|
||||
status = StatusField()
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ from model_utils.tests.models import (
|
|||
InheritanceManagerTestChild2, TimeStamp, Post, Article, Status,
|
||||
StatusPlainTuple, TimeFrame, Monitored, StatusManagerAdded,
|
||||
TimeFrameManagerAdded, Dude, SplitFieldAbstractParent, Car, Spot,
|
||||
Tracked, TrackedNotDefault, TrackedMultiple)
|
||||
Tracked, TrackedNotDefault, TrackedMultiple, StatusFieldDefaultFilled,
|
||||
StatusFieldDefaultNotFilled)
|
||||
|
||||
|
||||
|
||||
|
|
@ -164,6 +165,16 @@ class MonitorFieldTests(TestCase):
|
|||
self.assertRaises(TypeError, MonitorField)
|
||||
|
||||
|
||||
class StatusFieldTests(TestCase):
|
||||
|
||||
def test_status_with_default_filled(self):
|
||||
instance = StatusFieldDefaultFilled()
|
||||
self.assertEquals(instance.status, instance.STATUS.yes)
|
||||
|
||||
def test_status_with_default_not_filled(self):
|
||||
instance = StatusFieldDefaultNotFilled()
|
||||
self.assertEquals(instance.status, instance.STATUS.no)
|
||||
|
||||
|
||||
class ChoicesTests(TestCase):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue