Skip to content

Commit

Permalink
feat: 增加NumberField
Browse files Browse the repository at this point in the history
  • Loading branch information
Guo-Zhang committed Jul 7, 2023
1 parent b8feca6 commit a9685c6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
4 changes: 3 additions & 1 deletion django_quanttide/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,6 +17,7 @@
'Model',
'PolymorphicModel',
'IDField',
'NumberField',
'NameField',
'VerboseNameField',
'TitleField',
Expand Down
26 changes: 26 additions & 0 deletions django_quanttide/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
标识字段
Expand Down
6 changes: 5 additions & 1 deletion tests/models.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
22 changes: 20 additions & 2 deletions tests/test_models_fields.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Expand Down

0 comments on commit a9685c6

Please sign in to comment.