문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| python:django:models [2022/07/12 14:38] – [Relationship fields] taekgu | python:django:models [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1 | ||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| + | ====== Django Models ====== | ||
| + | ===== Models ===== | ||
| + | [[https:// | ||
| + | ===== Field ===== | ||
| + | [[https:// | ||
| + | |||
| + | ==== Field options ==== | ||
| + | === Field.null === | ||
| + | True면 null허용 - Database에 Null값저장여부 | ||
| + | |||
| + | === Field.blank === | ||
| + | True면 blank허용, | ||
| + | True 인 경우 필드를 비워 둘 수 있습니다. | ||
| + | 이것은 null과 다른데요. null은 순전히 데이터베이스와 관련이있는 반면 blank는 유효성 검사와 관련됩니다. | ||
| + | |||
| + | === Field.choices === | ||
| + | <code python> | ||
| + | YEAR_IN_SCHOOL_CHOICES = [ | ||
| + | (' | ||
| + | (' | ||
| + | (' | ||
| + | (' | ||
| + | (' | ||
| + | ] | ||
| + | </ | ||
| + | |||
| + | === Filed.db_column === | ||
| + | Database column의 이름 | ||
| + | |||
| + | === Field.db_index === | ||
| + | True면 필드에 대한 database index를 생성한다. | ||
| + | |||
| + | === Field.db_tablespace === | ||
| + | 이 field의 index에 대한 tablespace name | ||
| + | |||
| + | === Field.default === | ||
| + | default값 | ||
| + | |||
| + | === Field.editable === | ||
| + | False면 Admin, ModelForm에 보여지지 않는다. model validation또한 skip. | ||
| + | default로 True. | ||
| + | |||
| + | === Field.error_messages === | ||
| + | The error_messages argument lets you override the default messages that the field will raise. | ||
| + | |||
| + | === Field.help_text === | ||
| + | Form field에 extra help | ||
| + | |||
| + | === Field.primary_key === | ||
| + | True - primary key | ||
| + | |||
| + | === Field.unique === | ||
| + | If True, this field must be unique throughout the table. | ||
| + | |||
| + | === Field.unique_for_date === | ||
| + | Set this to the name of a DateField or DateTimeField to require that this field be unique for the value of the date field. | ||
| + | |||
| + | === Field.unique_for_month === | ||
| + | Like unique_for_date, | ||
| + | |||
| + | === Field.unique_for_year === | ||
| + | Like unique_for_date and unique_for_month. | ||
| + | |||
| + | === Field.verbose_name === | ||
| + | A human-readable name for the field. | ||
| + | |||
| + | === Field.validators === | ||
| + | A list of validators to run for this field. | ||
| + | |||
| + | ==== Field types ==== | ||
| + | 쓸데없이 왜 이렇게 많은 거야! | ||
| + | |||
| + | === AutoField === | ||
| + | |||
| + | === BigAutoField === | ||
| + | |||
| + | === BigIntegerField === | ||
| + | |||
| + | === CharField === | ||
| + | * CharField.max_length | ||
| + | * DecimalField.decimal_places | ||
| + | === DateField === | ||
| + | |||
| + | === DateTimeField === | ||
| + | |||
| + | === DecimalField === | ||
| + | * DecimalField.max_digits | ||
| + | === IntegerField === | ||
| + | |||
| + | === TextField === | ||
| + | |||
| + | === TimeField === | ||
| + | |||
| + | ==== Relationship fields ==== | ||
| + | === ForeignKey === | ||
| + | * to_field | ||
| + | * on_delete | ||
| + | |||
| + | <code python> | ||
| + | if field.name == ' | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | if isinstance(field, | ||
| + | print(f" | ||
| + | print(item.code_kind.__class__.objects.get(code_kind=' | ||
| + | print(item.code_kind.__class__.objects.get(code_kind=' | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | print(f" | ||
| + | pass | ||
| + | print(field.key) | ||
| + | print(item.code_kind) | ||
| + | print(type(item.code_kind)) | ||
| + | print(item.code_kind.objects.get(code_kind=' | ||
| + | |||
| + | item.code_kind = ' | ||
| + | else: | ||
| + | print(field.name) | ||
| + | </ | ||
| + | === ManyToManyField === | ||
| + | |||
| + | === OneToOneField === | ||
| + | |||
| + | |||
| + | ==== Field API reference ==== | ||
| + | |||
| + | === deconstruct() === | ||
| + | |||
| + | Returns a 4-tuple with enough information to recreate the field: | ||
| + | |||
| + | - The name of the field on the model. | ||
| + | - The import path of the field (e.g. " | ||
| + | - A list of positional arguments. | ||
| + | - A dict of keyword arguments. | ||
| + | This method must be added to fields prior to 1.7 to migrate its data using Migrations. | ||
| + | ==== Field attribute reference ==== | ||
| + | |||
| + | === Attributes for fields === | ||
| + | * Field.auto_created | ||
| + | * Field.concrete | ||
| + | * Field.hidden | ||
| + | * Field.is_relation | ||
| + | * Field.model | ||
| + | |||
| + | === Attributes for fields with relations === | ||
| + | * Field.many_to_many | ||
| + | * Field.many_to_one | ||
| + | * Field.one_to_many | ||
| + | * Field.one_to_one | ||
| + | * **Field.related_model** - Points to the model the field relates to. | ||