您的位置:首頁技術文章
文章詳情頁

django自定義非主鍵自增字段類型詳解(auto increment field)

瀏覽:20日期:2024-10-16 11:14:26

1.django自定義字段類型,實現非主鍵字段的自增

# -*- encoding: utf-8 -*-from django.db.models.fields import Field, IntegerFieldfrom django.core import checks, exceptionsfrom django.utils.translation import ugettext_lazy as _class AutoIncreField(Field): description = _('Integer') empty_strings_allowed = False default_error_messages = { ’invalid’: _('’%(value)s’ value must be an integer.'), } def __init__(self, *args, **kwargs): kwargs[’blank’] = True super(AutoIncreField, self).__init__(*args, **kwargs) def check(self, **kwargs): errors = super(AutoIncreField, self).check(**kwargs) # 每張表只能設置一個字段為自增長字段,這個字段可以是主鍵,也可以不是主鍵,如果不是主鍵,則必須設置為一種“鍵(key)” # (primary key)也是鍵(key)的一種,key還包括外鍵(foreign key)、唯一鍵(unique key) errors.extend(self._check_key()) return errors def _check_key(self): if not self.unique: return [ checks.Error( ’AutoIncreFields must set key(unique=True).’, obj=self, id=’fields.E100’, ), ] else: return [] def deconstruct(self): name, path, args, kwargs = super(AutoIncreField, self).deconstruct() del kwargs[’blank’] kwargs[’unique’] = True return name, path, args, kwargs def get_internal_type(self): return 'AutoIncreField' def to_python(self, value): if value is None: return value try: return int(value) except (TypeError, ValueError): raise exceptions.ValidationError( self.error_messages[’invalid’], code=’invalid’, params={’value’: value}, ) def db_type(self, connection): return ’bigint AUTO_INCREMENT’ def rel_db_type(self, connection): return IntegerField().db_type(connection=connection) def validate(self, value, model_instance): pass def get_db_prep_value(self, value, connection, prepared=False): if not prepared: value = self.get_prep_value(value) value = connection.ops.validate_autopk_value(value) return value def get_prep_value(self, value): value = super(AutoIncreField, self).get_prep_value(value) if value is None: return None return int(value) def contribute_to_class(self, cls, name, **kwargs): assert not cls._meta.auto_field, 'A model can’t have more than one AutoIncreField.' super(AutoIncreField, self).contribute_to_class(cls, name, **kwargs) cls._meta.auto_field = self def formfield(self, **kwargs): return None

2.使用

class Test(models.Model): id = models.UUIDField(primary_key=True, default=uuid4) numbering = AutoIncreField(_(u’numbering’), unique=True) name = models.CharField(_(u’name’), max_length=32, blank=True, null=True)

3.bug

當save()后并不能刷新instance,及save后numbering會為空值,需要重寫get一次.

如果您修復了這個問題請留言回復下,謝謝

4.bug修復

以一種非常不優雅的方法進行了簡單修復,重寫了模型的save方法,在save后從新get

class AutoIncreFieldFixMinxin(object): def save(self, *args, **kwargs): super(AutoIncreFieldFixMinxin, self).save(*args, **kwargs) auto_field = self._meta.auto_field.name new_obj = self.__class__.objects.get(pk=self.pk) setattr(self, auto_field, int(getattr(new_obj, auto_field)))class Test(AutoIncreFieldFixMinxin, models.Model): id = models.UUIDField(primary_key=True, default=uuid4) sequence = AutoIncreField(_(u’sequence’), unique=True) name = models.CharField(_(u’name’), max_length=100)

補充知識:Django model 表與表的關系

一對多:models.ForeignKey(其他表)

多對多:models.ManyToManyField(其他表)

一對一:models.OneToOneField(其他表)

應用場景:

一對多:當一張表中創建一行數據時,有一個單選的下拉框(可以被重復選擇)

例如:創建用戶信息時候,需要選擇一個用戶類型【普通用戶】【金牌用戶】【鉑金用戶】等。

多對多:在某表中創建一行數據是,有一個可以多選的下拉框

例如:創建用戶信息,需要為用戶指定多個愛好

一對一:在某表中創建一行數據時,有一個單選的下拉框(下拉框中的內容被用過一次就消失了

例如:原有含10列數據的一張表保存相關信息,經過一段時間之后,10列無法滿足需求,需要為原來的表再添加5列數據

ForeignKey(ForeignObject) # ForeignObject(RelatedField) to, # 要進行關聯的表名 to_field=None, # 要關聯的表中的字段名稱 on_delete=None, # 當刪除關聯表中的數據時,當前表與其關聯的行的行為 - models.CASCADE,刪除關聯數據,與之關聯也刪除 - models.DO_NOTHING,刪除關聯數據,引發錯誤IntegrityError - models.PROTECT,刪除關聯數據,引發錯誤ProtectedError - models.SET_NULL,刪除關聯數據,與之關聯的值設置為null(前提FK字段需要設置為可空) - models.SET_DEFAULT,刪除關聯數據,與之關聯的值設置為默認值(前提FK字段需要設置默認值) - models.SET,刪除關聯數據, a. 與之關聯的值設置為指定值,設置:models.SET(值) b. 與之關聯的值設置為可執行對象的返回值,設置:models.SET(可執行對象) def func():return 10 class MyModel(models.Model):user = models.ForeignKey(to='User',to_field='id'on_delete=models.SET(func),) related_name=None, # 反向操作時,使用的字段名,用于代替 【表名_set】 如: obj.表名_set.all() related_query_name=None, # 反向操作時,使用的連接前綴,用于替換【表名】 如: models.UserGroup.objects.filter(表名__字段名=1).values(’表名__字段名’) limit_choices_to=None, # 在Admin或ModelForm中顯示關聯數據時,提供的條件: # 如: - limit_choices_to={’nid__gt’: 5} - limit_choices_to=lambda : {’nid__gt’: 5} from django.db.models import Q - limit_choices_to=Q(nid__gt=10) - limit_choices_to=Q(nid=8) | Q(nid__gt=10) - limit_choices_to=lambda : Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption=’root’) db_constraint=True # 是否在數據庫中創建外鍵約束 parent_link=False # 在Admin中是否顯示關聯數據 OneToOneField(ForeignKey) to, # 要進行關聯的表名 to_field=None # 要關聯的表中的字段名稱 on_delete=None, # 當刪除關聯表中的數據時,當前表與其關聯的行的行為 ###### 對于一對一 ###### # 1. 一對一其實就是 一對多 + 唯一索引 # 2.當兩個類之間有繼承關系時,默認會創建一個一對一字段 # 如下會在A表中額外增加一個c_ptr_id列且唯一: class C(models.Model): nid = models.AutoField(primary_key=True) part = models.CharField(max_length=12) class A(C): id = models.AutoField(primary_key=True) code = models.CharField(max_length=1) ManyToManyField(RelatedField) to, # 要進行關聯的表名 related_name=None, # 反向操作時,使用的字段名,用于代替 【表名_set】 如: obj.表名_set.all() related_query_name=None, # 反向操作時,使用的連接前綴,用于替換【表名】 如: models.UserGroup.objects.filter(表名__字段名=1).values(’表名__字段名’) limit_choices_to=None, # 在Admin或ModelForm中顯示關聯數據時,提供的條件: # 如: - limit_choices_to={’nid__gt’: 5} - limit_choices_to=lambda : {’nid__gt’: 5} from django.db.models import Q - limit_choices_to=Q(nid__gt=10) - limit_choices_to=Q(nid=8) | Q(nid__gt=10) - limit_choices_to=lambda : Q(Q(nid=8) | Q(nid__gt=10)) & Q(caption=’root’) symmetrical=None, # 僅用于多對多自關聯時,symmetrical用于指定內部是否創建反向操作的字段 # 做如下操作時,不同的symmetrical會有不同的可選字段 models.BB.objects.filter(...) # 可選字段有:code, id, m1 class BB(models.Model): code = models.CharField(max_length=12) m1 = models.ManyToManyField(’self’,symmetrical=True) # 可選字段有: bb, code, id, m1 class BB(models.Model): code = models.CharField(max_length=12) m1 = models.ManyToManyField(’self’,symmetrical=False) through=None, # 自定義第三張表時,使用字段用于指定關系表 through_fields=None, # 自定義第三張表時,使用字段用于指定關系表中那些字段做多對多關系表 from django.db import models class Person(models.Model): name = models.CharField(max_length=50) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField( Person, through=’Membership’, through_fields=(’group’, ’person’), ) class Membership(models.Model): group = models.ForeignKey(Group, on_delete=models.CASCADE) person = models.ForeignKey(Person, on_delete=models.CASCADE) inviter = models.ForeignKey( Person, on_delete=models.CASCADE, related_name='membership_invites', ) invite_reason = models.CharField(max_length=64) db_constraint=True, # 是否在數據庫中創建外鍵約束 db_table=None, # 默認創建第三張表時,數據庫中表的名稱

ForeignKey外鍵(跨表操作):

跨表操作1

v = models.Host.objects.filter(nid__gt=0)

v[0].b.caption #通過.進行跨表操作,在對象中去做跨表操作用.

跨表操作2

v = models.Host.objects.filter(nid__gt=0).values(’nid’,’hostname’,’b_id’,’b__caption’) #使用values()取值時可以用雙下劃線做跨表操作

for row in v:print(row[’nid’],row[’hostname’],row[’b_id’],row[’b__caption’])

前端:

<td>{{ row.b__caption }}</td> # 用雙下劃線做跨表操作

以上這篇django自定義非主鍵自增字段類型詳解(auto increment field)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
国产综合久久一区二区三区