1

I have a field called subjects that asks users how many subjects do they have and based on the number they input I want to generate the input fields of same number. And How and where do I store those inputs.

MODELS.PY

#this field will determine how many input fields to generate
subjects = models.IntegerField()

VIEWS.PY

def generate_forms(request):
    no_of_fields = request.GET.get('subjects')
    if no_of_fields:
        #generate other inupts
        #save it in the database

Besides generating the input, how do I save those data in the database. Thanks in advance

1
  • if you are using postgresql i suggest create a json column based on your subject input save each value as key value.. example if your number of subjects is 3 and you save something like this--> {'subject':1,'subject2':4,'subject3':3} or create a array column which saved as list.. like [1,2,3].. Commented Feb 26, 2018 at 14:20

1 Answer 1

1

If you use postgres you can use django postgres specefic models fields(Like ArrayField).django specefic fields documention

For another databases you can create model for your subjects and for each subject you can insert new data in Subject model.

class Subject(models.Model):
    desc = models.CharField(max_length=50)
    other_filed = models.ForeignKey(OtherModel)

def generate_forms(request):
    other_field = 1
    subjects = request.GET.get('subjects')
    if subjects and subjects != '':
        for subject in subjects:
            Subject.objects.create(desc=subject, other_field=other_field)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.