사용자 도구

사이트 도구


python:django:models

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
python:django:models [2022/07/12 14:38] – [Relationship fields] taekgupython:django:models [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== Django Models ======
 +===== Models =====
 +[[https://docs.djangoproject.com/en/4.0/topics/db/models/|Models]]
 +===== Field =====
 +[[https://docs.djangoproject.com/en/4.0/ref/models/fields/|Model field reference]]
 +
 +==== Field options ====
 +=== Field.null ===
 +True면 null허용 - Database에 Null값저장여부
 +
 +=== Field.blank ===
 +True면 blank허용, Field.null은 False
 +True 인 경우 필드를 비워 둘 수 있습니다. 
 +이것은 null과 다른데요. null은 순전히 데이터베이스와 관련이있는 반면 blank는 유효성 검사와 관련됩니다.
 +
 +=== Field.choices ===
 +<code python>
 +YEAR_IN_SCHOOL_CHOICES = [
 +    ('FR', 'Freshman'),
 +    ('SO', 'Sophomore'),
 +    ('JR', 'Junior'),
 +    ('SR', 'Senior'),
 +    ('GR', 'Graduate'),
 +]
 +</code>
 +
 +=== 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, but requires the field to be unique with respect to the month.
 +
 +=== 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 == 'code_kind':
 +                print(f"{field.name} <- MARO_DOMAIN_CD")
 +                print(f"{field.attname} <- attname")
 +                print("ForeignKey : {}.".format(isinstance(field, models.ForeignKey)))
 +                print("ManyToManyField : {}.".format(isinstance(field, models.ManyToManyField)))
 +                print("OneToOneField : {}.".format(isinstance(field, models.OneToOneField)))
 +                print("Field : {}.".format(isinstance(field, models.Field)))
 +                if isinstance(field, models.ForeignKey):
 +                    print(f"field.deconstruct 3:{ field.deconstruct()[3] }")
 +                    print(item.code_kind.__class__.objects.get(code_kind='MARO_DOMAIN_CD'))
 +                    print(item.code_kind.__class__.objects.get(code_kind='LANG_CD'))
 +                    print(f"field.deconstruct:{ field.deconstruct() }")
 +                    print(f"target_field:{ field.target_field }")
 +                    print(f"db_constraint:{ field.db_constraint }")
 +                    print(f"field.db_column(현 테이블의 column명):{ field.db_column }")
 +                    pass
 +                print(field.key)
 +                print(item.code_kind)
 +                print(type(item.code_kind))
 +                print(item.code_kind.objects.get(code_kind='MARO_DOMAIN_CD'))
 +
 +                item.code_kind = 'MARO_DOMAIN_CD'
 +            else:
 +                print(field.name)
 +</code>
 +=== 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. "django.db.models.IntegerField"). This should be the most portable version, so less specific may be better.
 +  - 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.