diff --git a/django_quanttide/models/__init__.py b/django_quanttide/models/__init__.py index c75646e..cfe9023 100644 --- a/django_quanttide/models/__init__.py +++ b/django_quanttide/models/__init__.py @@ -4,7 +4,8 @@ from .choices import StageChoices from .fields import ( - IDField, NameField, VerboseNameField, TitleField, DescriptionField, + IDField, NumberField, NameField, + VerboseNameField, TitleField, DescriptionField, TypeField, StatusField, StageField, CreatedAtField, UpdatedAtField, CreatedByField, UpdatedByField, @@ -16,6 +17,7 @@ 'Model', 'PolymorphicModel', 'IDField', + 'NumberField', 'NameField', 'VerboseNameField', 'TitleField', diff --git a/django_quanttide/models/fields.py b/django_quanttide/models/fields.py index 7a553bf..d7cacba 100644 --- a/django_quanttide/models/fields.py +++ b/django_quanttide/models/fields.py @@ -37,6 +37,32 @@ def __init__(self, **options): super().__init__(**options) +class NumberField(models.IntegerField): + """ + 编号字段 + """ + description = "编号字段" + + def __init__(self, **options): + options['editable'] = False + options['unique'] = True + options.setdefault('verbose_name', '编号') + super().__init__(**options) + + def pre_save(self, model_instance, add): + value = getattr(model_instance, self.attname) + if not value: + # 如果字段值为空,则生成一个自增的值 + queryset = model_instance.__class__.objects.all() + if queryset: + last_object = queryset.latest(self.attname) + value = getattr(last_object, self.attname) + 1 + else: + value = 1 + setattr(model_instance, self.attname, value) + return value + + class NameField(models.SlugField): """ 标识字段 diff --git a/tests/models.py b/tests/models.py index 5756cf0..73983e2 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,8 +1,12 @@ from django_quanttide import models +class ExampleNumberModel(models.Model): + number = models.NumberField() + + class ExampleModel(models.Model): - # number = models.NumberField() + number = models.NumberField() name = models.NameField() verbose_name = models.VerboseNameField() title = models.TitleField() diff --git a/tests/test_models.py b/tests/test_models.py index 9e7bb4c..bddcce8 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -24,7 +24,7 @@ def setUp(self): def test_defaults(self): example = self.example - # self.assertEqual(example.number, 1) + self.assertEqual(example.number, 1) self.assertEqual(example.name, 'example') self.assertEqual(example.verbose_name, 'example instance') self.assertEqual(example.title, 'Example Model') diff --git a/tests/test_models_fields.py b/tests/test_models_fields.py index 5801a45..1cb2655 100644 --- a/tests/test_models_fields.py +++ b/tests/test_models_fields.py @@ -1,15 +1,18 @@ import uuid -from django.test import SimpleTestCase, modify_settings +from django.test import SimpleTestCase, TestCase, modify_settings from django.contrib.auth import get_user_model from django_quanttide.models import ( - IDField, NameField, VerboseNameField, TitleField, DescriptionField, + IDField, NumberField, NameField, + VerboseNameField, TitleField, DescriptionField, TypeField, StatusField, StageField, StageChoices, CreatedAtField, UpdatedAtField, CreatedByField, UpdatedByField, ) +from tests.models import ExampleNumberModel + class IDFieldTestCase(SimpleTestCase): def test_defaults(self): @@ -27,6 +30,21 @@ def test_custom_options(self): self.assertIsInstance(field.default(), uuid.UUID) +class NumberFieldTestCase(TestCase): + def test_init(self): + field = NumberField() + self.assertFalse(field.editable) + self.assertTrue(field.unique) + + def test_pre_save(self): + # 表为空 + instance = ExampleNumberModel.objects.create() + self.assertEqual(1, instance.number) + # 表存在值 + instance2 = ExampleNumberModel.objects.create() + self.assertEqual(2, instance2.number) + + class NameFieldTestCase(SimpleTestCase): def test_defaults(self): field = NameField()