Django_Models field type and simple operation

Posted by fernyburn on Thu, 10 Oct 2019 21:02:24 +0200

# [1] Field
# AutoField() self-increasing type, usually used on id, is not specified, and gradually ID is automatically added to the model
# CharField(max_length=?) Character type must be given maximum length. The default form style is Text Input.
# TextField() large text type, > 4000, default form space is Textarea
# IntegerField() Integer type
# FloatField() Floating Point Type
# DateField([auto_now=?,auto_now_add=?]) The date type y-m-d list is an optional attribute, the first is the update time, and the second is the creation time.
# DateTimeField() Date-time type y-m-d h:m:s
# BooleanField() Boolean type True False default form control Checkbox Input
# NullBooleanField() can be an empty Boolean type Null True False
# DecimalField(max_digits=None,decimal_places=None) specifies decimal type, decimal floating point number (total, decimal)
# ImageField() needs to install the package pip install pillow

# [2] Field parameters
# primary_key = True sets the primary key
# unique = True must be unique
# default =''default to be empty must be an empty string
# Verbose_name = "." ADMIN field alias displayed in the background
# If this property is added to the field name displayed in the name database, when customizing admin, the name defined in list_display() is used, but it does not affect the query.
# null = True database allowed to be empty
# Blank = True ADMIN background add data allowed to be empty
# ! General null = True & blank = True is used at the same time
# max_length = 20 maximum length
# db_index = True index
# editable = True is editable
# auto_now update time (variable)
# auto_now_add insertion time (immutable)

# choices custom enumeration type
'''
USER_TYPE_LIST = (
    (1, 'Ordinary users'),
    (2, 'VIP'),
)
user_type = models.IntegerField(choices=USER_TYPE_LIST, default=1, verbose_name = "customer type")

'''
# set a table name
# class Meta:
#     db_table = "person"
  1. Single-form operation of model
    Add:

    1. stu = Student()
      stu.name = ""
      stu.save()
    2. stu = Student(name='')
      stu.save()
    3. Student.objects.create(name='')
    4. Student.objects.get_or_create(name='')

    Delete

    Student.objects.filter(name='').delete()
    Student.objects.get(id=1).delete()

    change

    Student.objects.filter(name='').update(name='2')
    stu = Student.objects.get(id=1)
    stu.name = '2'
    # stu.save()
    stu.save(update_fields=['name'])

    check

    Student.objects.get(): Only one data (object) can be found.

    If you find multiple data, you will report an error: MultipleObjects Return.
    If you find 0 data, you will report an error: DoesNotExist

    Student.objects.all(): All data (query set)
    Student.objects.filter(): Conditional query data (query set)
    first(): the first data
    last(): last data
    count(): Number of data bars in the query set
    exists(): Is there data in the query set?
    values(): Returns a list containing the dictionary (fields and values in the dictionary)
    values_list(): Returns a list containing tuples (only values in tuples)

Topics: Database Attribute pip