Coder Social home page Coder Social logo

survey-management's Introduction

header

Hi There! 🙋‍♂️

survey-management's People

Contributors

tasddc1226 avatar

Watchers

 avatar  avatar

survey-management's Issues

#1 answer_type이 CheckboxSelectMultiple인 경우 DB에 적재하는 방법?

## models.py

class Survey(models.Model):
    """설문지는 관리자에 의해 생성"""

    author = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    is_active = models.BooleanField(default=False)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        db_table = "survey"


class Question(models.Model):
    """
    하나의 설문지에 여러 질문이 존재
    질문의 유형(type)은  Radio(1개), Checkbox(1개 이상), Select(1개) 중 하나
    """

    RADIOBUTTON = 1
    MULTIPLE = 2
    SELECT = 3
    TYPES_CHOICES = [
        (RADIOBUTTON, "RadioButton"),
        (MULTIPLE, "CheckBox"),
        (SELECT, "Select"),
    ]
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    question_text = models.CharField(max_length=100)
    answer_type = models.IntegerField(
        choices=TYPES_CHOICES, default=RADIOBUTTON, max_length=32
    )

    class Meta:
        db_table = "question"


class Choice(models.Model):
    """
    하나의 질문에는 여러 보기가 존재
    """

    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=100)

    class Meta:
        db_table = "choice"


class Submission(models.Model):
    """특정 설문지 질문들에 대한 답변 집합"""

    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)
    is_complete = models.BooleanField(default=False)

    class Meta:
        db_table = "submission"


class Answer(models.Model):
    """특정 질문에 대한 답변"""

    submission = models.ForeignKey(Submission, on_delete=models.CASCADE)
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE)

    class Meta:
        db_table = "answer"
  • 모델링은 위와 같이 정의하였으며, 각 table마다 주석으로 설명을 해두었음.
## forms.py의 AnswerForm class
if type == 1:
    choice_field = forms.ChoiceField(
        choices=lists, widget=forms.RadioSelect, required=True
    )
elif type == 2:
    choice_field = forms.ChoiceField(
        choices=lists,
        widget=forms.CheckboxSelectMultiple,
        required=True,
    )
elif type == 3:
    choice_field = forms.ChoiceField(
        choices=lists, widget=forms.Select, required=True
    )
  • 질문의 응답 형식(Question.answer_type)에 따라서 보여질 부분이 바뀌도록 설정하는 부분
mysql> select * from answer;
+----+-----------+---------------+
| id | choice_id | submission_id |
+----+-----------+---------------+
|  2 |        93 |             7 |
|  3 |        97 |             7 |
|  4 |        71 |             8 |
|  5 |        73 |             8 |
|  6 |        80 |             8 |
+----+-----------+---------------+
5 rows in set (0.00 sec)
  • RadioSelectSelect일 때에는 한 개씩 매칭이 되므로 위와 같이 테이블에 적재 가능.
  • CheckboxSelectMultiple인 경우 아래와 같이 선택된 데이터가 list 형태이다.

Screen Shot 2022-06-08 at 17 19 34

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.