Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据输入生成输入字段并正确存储_Python_Django - Fatal编程技术网

Python 根据输入生成输入字段并正确存储

Python 根据输入生成输入字段并正确存储,python,django,Python,Django,我有一个名为“主题”的字段,询问用户他们有多少主题,并根据他们输入的数字生成相同数字的输入字段。以及如何以及在何处存储这些输入 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('subject

我有一个名为“主题”的字段,询问用户他们有多少主题,并根据他们输入的数字生成相同数字的输入字段。以及如何以及在何处存储这些输入

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
除了生成输入,我如何将这些数据保存在数据库中。
提前感谢

如果您使用postgres,您可以使用django postgres specefic models字段(如ArrayField)

对于其他数据库,您可以为主题创建模型,对于每个主题,您可以在主题模型中插入新数据

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)

如果您使用的是postgresql,我建议您根据主题输入创建一个json列,并将每个值保存为键值。。例如,如果主题数为3,并且保存如下内容-->{'subject':1,'subject2':4,'subject3':3},或者创建一个保存为列表的数组列。。像[1,2,3]。。