Django ORM에서 많이 쓰는 Field 입니다.

18986 단어 djangoDjangoORM
Django ORM에서 많이 쓰는 Field 입니다.
  • 1:django모델 모델 모델 상용 필드
  • 2:django모델 모델 필드 상용 매개 변수
  • 3: Django의 기본 id 키 대신 UUID를 사용합니다
  • 1:django 모형 모델 상용 필드
    1:models.AutoField
           = int(11)id   
                     ,    primary_key=True

    2:models.CharField
             ;    max_length  
    

    3:models.BooleanField
           =tinyint(1);    ,   Blank=True
    

    4:models.ComaSeparatedIntegerField
               =varchar;  CharField,     max_lenght   
    

    5:models.DateField
            date
      DateField.auto_now:               ,      
       DateField.auto_now_add:                       ,    。
    

    6:models.DateTimeField
    	       datetime; DateField   
    

    7:models.Decimal
               = decimal
       DecimalField.max_digits:          
       DecimalField.decimal_places:        
    

    8:models.EmailField
    	       Email      CharField
    

    9:models.FloatField
    	     = double
    

    10:models.IntegerField
    	  
    

    11:models.BigIntegerField
          
      integer_field_ranges = {
        'SmallIntegerField': (-32768, 32767),
        'IntegerField': (-2147483648, 2147483647),
        'BigIntegerField': (-9223372036854775808, 9223372036854775807),
        'PositiveSmallIntegerField': (0, 32767),
        'PositiveIntegerField': (0, 2147483647),
      }
    

    12:models.GenericIPAddressField
    	       IP       CharField
    

    13:models.NullBooleanField
    	         
    

    14:models.PositiveIntegerFiel
     	   
    

    15:models.PositiveSmallIntegerField
    	 smallInteger
    

    16:models.SlugField
    	  、   、  、  
    

    17:models.SmallIntegerField
    	  ;        :tinyint、smallint、int、bigint
    

    18:models.TextField
    	   。     form   textarea。
    

    19:models.TimeField
    	   HH:MM[:ss[.uuuuuu]]
    

    20:models.URLField
    	    URL      CharField
    

    21:models.BinaryField
    	   ;       。    filter    QuerySet。
    

    22:models.ImageField
         
       ImageField.height_field、ImageField.width_field:         ,                 。
             Python Imaging  Pillow。
                        。
    

    23:models.FileField(upload_to=None[, max_length=100, ** options])
    	  
       FileField.upload_to:                   ,     MEDIA_ROOT    
               primary_key unique  .          varchar,       100
    

    24:models.FilePathField(path=None[, math=None, recursive=False, max_length=100, **options])
       FilePathField.path:       ,  
       FilePathField.match:                ,           (     )。
       FilePathField.recursive:True   FalseFalse,               。
         :FilePathField(path="/home/images", match="foo.*", recursive=True)/home/images/foo.gif”    “/home/images/foo/bar.gif”  
    

    2:django 모델 모델 필드 상용 매개 변수
    1、null
    	   True,Django              NULL,    False
    

    2、blank
    	   True django  Admin            ,    。   FalseFalse。
    	null           。 blank            
    

    3、primary_key = False
    	  , AutoField     ,          id  
    

    4、auto_now 및 autonow_add
    	auto_now       ---       ,         
    	auto_now_add      ---         
    

    5、choices
    	          choices,      ,Django select box        ,    choices        
      GENDER_CHOICE = (
            (u'M', u'Male'),
            (u'F', u'Female'),
      )
      gender = models.CharField(max_length=2,choices = GENDER_CHOICE)
    

    6、max_length
    	    
    

    7、default
    	   
    

    8、verbose_name
    	Admin        ,         ,     。
    

    9、db_column
    	         
    

    10、unique=True
    	     
    

    11、db_index = True
    	     
    

    12、editable=True
    	 Admin      
    

    13、error_messages=None
    	    
    

    14、auto_created=False
    	    
    

    15、help_text
    	 Admin       
    

    16、validators=[]
    	   
    

    17、upload-to
    	               
    

    3: Django의 기본 id 키 대신 UUID를 사용합니다.
    먼저 UUID 플러그인 패키지를 설치합니다.
    pip install django-shortuuidfield
    
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
    from django.contrib.auth.models import PermissionsMixin
    from django.db import models
    from shortuuidfield import ShortUUIDField
    
    class UserManager(BaseUserManager):
        def _create_user(self, mobile, username, password, **kwargs):
            if not all([mobile, username, password]):
                raise ValueError('mobile, username, password all required')
            user = self.model(mobile=mobile, username=username, **kwargs)
            user.set_password(password)
            user.save()
            return user
        def create_user(self, mobile, username, password, **kwargs):
            kwargs['is_superuser'] = False
            return self._create_user(mobile, username, password, **kwargs)
        def create_superuser(self, mobile, username, password, **kwargs):
            kwargs['is_superuser'] = True
            kwargs['is_staff'] = True
            return self._create_user(mobile, username, password, **kwargs)
    
    class UserProfile(AbstractBaseUser, PermissionsMixin):
        uid = ShortUUIDField(primary_key=True)
        mobile = models.CharField(max_length=11, unique=True)
        email = models.EmailField(unique=True)
        username = models.CharField(max_length=100)
        is_active = models.BooleanField(default=True)
        is_staff = models.BooleanField(default=False)
        date_joined = models.DateTimeField(auto_now_add=True)
        # USERNAME_FIELD:      authenticate         username=username,     USERNAME_FIELD mobile,     username = mobile
        USERNAME_FIELD = 'mobile'
        # REQUIRED_FIELDS:     createsuperuser         ,  ,       USERNAME_FIELD  password  
        #      username  ,             mobile,username,password    
        REQUIRED_FIELDS = ['username']
        objects = UserManager()
    
        def get_full_name(self):
            return self.username
        def get_short_name(self):
            return self.username
    

    좋은 웹페이지 즐겨찾기