Models in Django

Introduction

Here's the basics of Django's model.

settings in models.py

models.py


from django.db import models


class SampleModel(models.Model):
    # id = models.AutoField(primary_key=True)
    char_sample = models.CharField(max_length=200)
    char_with_null = models.CharField(max_length=10, blank=True, null=True)
    char_options = models.CharField(choices=(('one', 1), ('two', 2)))
    text_sample = models.TextField()
    bool_sample = models.BooleanField(default=False)
    datetime_created = models.DateTimeField(auto_now_add=True)
    datetime_updated = models.DateTimeField(auto_now=True)
    date_sample = models.DateField()

    def __str__(self):
        return self.char_sample

Primary key

The primary key is set automatically, even if you don't specify it. In views etc., you can refer to it in the form of pk etc.

CharField

A field for entering characters. HTML tags correspond to <input type =" text ">. max_length (maximum number of characters) must always be specified. Also, if blank = True, blank is allowed at the time of validation, and if null = True, null is allowed at the time of data registration. blank = and null = can be used in other fields as well.

TextField

This is also a field for entering characters. The HTML tag corresponds to <textarea>, and you can enter a large number of characters.

BooleanField

A field that accepts boolean values. If default = False is specified, it will be treated as False if it is not specified at the time of data registration. default = can also be used in other fields.

DateTimeField

A field for entering the date and time. If ʻauto_now_add = True, the date and time of that timing is registered only once at the beginning, and it is not updated after that. If ʻauto_now = True, the date and time of registration timing will be updated automatically.

DateField

A field for entering a date.

__str () __ function

The last __str__ function is written to refer to the specified field as the title instead of the primary key when referencing the model on the management screen.

admin.py settings

admin.py


from django.contrib import admin

from .models import SampleModel


admin.site.register(SampleModel)

Add ʻadmin.site.register (model name)` to refer to the model on the management screen.

Summary

Here, we explained the model-related settings. Next time, I will explain about views.

Recommended Posts

Models in Django
Forms in Django
Model changes in Django
Save multiple models in one form with Django
Performance optimization in Django 3.xx
PHP var_dump in Django templates
Handle constants in Django templates
Implement follow functionality in Django
Rename table columns in Django3
Output table structure in Django
Django
Dynamically create tables in schema with Django, dynamically generate models
(Note) Django in Vagrant environment
Create and list Django models
Show Django ManyToManyField in Template
Set placeholders in input fields in Django
8 Frequently Used Commands in Python Django
Dynamically add form fields in Django
Errors related to memcached in django
Sort django models by specific criteria
Implementation of login function in Django
Register your Django application in your project
Write foreign key constraints in Django
How to reflect CSS in Django
Switch the language displayed in Django 1.9
Get parameter values ​​in Django templates
Understand Kullback-Leibler spoken in generative models
The meaning of ".object" in Django
Deploy Django in 3 minutes using docker-compose
Pin factory_boy seed value in Django
GraphQL API with graphene_django in Django
Like button implementation in Django + Ajax
Get the query string (query string) in Django
Create a LINE Bot in Django
Install Django in a pipenv virtual environment
django update
Django note 4
Get the client's IP address in Django
When you can't call base.html in Django
Django: Migration doesn't reflect model in DB
Logical deletion in Django, DRF (Django REST Framework)
Django memorandum
What's new in Django 1.8 Conditional Expressions #djangoja
django search
Django installation
Django ~ Let's display it in the browser ~
Django Summary
Django test
Information recording memo using session in Django
How to delete expired sessions in Django
CSS environment created in 10 minutes using Django
Notes on creating static files in Django
Same-Site attribute setting for cookies in Django
Django # 2 (template)
(Note) Template file search order in Django
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics