django nested inline

1️⃣ requirement 설정

# requirements.txt
django-nested-inline>=0.4.4,<1.0.0

2️⃣ settings.py 설정

# settings.py
INSTALLED_APPS = [
    
    # third party apps
    ...
    'nested_inline',
    ...
]

3️⃣ 모델링


4️⃣ models.py

# models.py
class OkrBoard(models.Model):
    """OKR 관리보드"""
    team = models.CharField(max_length=20, verbose_name="팀 이름") 
    member = models.CharField(max_length=50, verbose_name="구성원", help_text="쉼표로 구분해주세요 ex)철수, 영희") 
    
    class Meta:
        db_table = 'okr_board'
        verbose_name = 'OKR 관리'
        verbose_name_plural = 'OKR 관리'

    def __str__(self):
        return self.team


class Objective(models.Model):
    """각 팀 목표"""

    name = models.CharField(max_length=200, verbose_name="목표")
    board = models.ForeignKey("OkrBoard", on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = "objective"
        verbose_name = "목표"
        verbose_name_plural = "목표"

    def __str__(self):
        return ""


class KeyResult(models.Model):
    """성과 지표"""

    name = models.CharField(max_length=500, verbose_name="성과 지표")
    objective = models.ForeignKey("Objective", on_delete=models.SET_NULL, null=True)
    current = models.DecimalField(max_digits=4, decimal_places=1, verbose_name="current", default=0)
    completed = models.DecimalField(max_digits=4, decimal_places=1, verbose_name="completed", default=0)
    goal = models.DecimalField(max_digits=4, decimal_places=1, verbose_name="goal", default=100)
    start_at = models.DateField(verbose_name="시작일")
    end_at = models.DateField(verbose_name="종료일")
    comment = models.CharField(max_length=500, verbose_name="코멘트", blank=True)
    status = models.ForeignKey("KeyResultStatus", on_delete=models.SET_NULL, null=True, verbose_name="상태")
    
    class Meta:
        db_table = "key_result"

    def __str__(self):
        return ""


class KeyResultStatus(models.Model):
    """
    성과 지표 진행 상태
    - Done, Working on it, Stuck
    """

    name = models.CharField(max_length=20, verbose_name="상태")

    class Meta:
        db_table = 'key_result_status'

    def __str__(self):
        return self.name

5️⃣ admin.py

# admin.py
from nested_inline.admin import NestedTabularInline, NestedStackedInline, NestedModelAdmin


class KeyResultInline(NestedTabularInline):
    """성과지표 인라인"""

    model = KeyResult
    fk_name = "objective"
    extra = 0
    # 어드민 필드 나열 순서
    fields = [
        "name",
        "start_at",
        "end_at",
        "current",
        "completed",
        "goal",
        "status",
        "comment",
    ]


class ObjectiveInline(NestedStackedInline):
    """목표 인라인"""

    model = Objective
    extra = 0
    fk_name = "board"
    inlines = [KeyResultInline]


@admin.register(OkrBoard)
class OkrBoardAdmin(NestedModelAdmin):
    """OKR 관리"""

    model = OkrBoard
    inlines = [ObjectiveInline]

6️⃣ 결과



7️⃣ 참고자료

https://github.com/s-block/django-nested-inline

좋은 웹페이지 즐겨찾기