如何为一个id创建10行10列的Django模型(主要)

问题描述 投票:0回答:1

想要这样的10x10

我正在尝试在 Django 中为一个用户开发一个 10 行和 10 列(文本字段)模型,以便他可以通过表单添加数据。有没有办法让它动态化?将来我可能需要添加 50 行和 50 列。

我尝试手动完成,但要做的时间太长了

imageNo_1 = models.CharField(max_length=50,blank=True,null=True)
imageNo_2 = models.CharField(max_length=50,blank=True,null=True)
imageNo_3 = models.CharField(max_length=50,blank=True,null=True)
imageNo_4 = models.CharField(max_length=50,blank=True,null=True)
imageNo_5 = models.CharField(max_length=50,blank=True,null=True)
imageNo_6 = models.CharField(max_length=50,blank=True,null=True)
imageNo_7 = models.CharField(max_length=50,blank=True,null=True)
imageNo_8 = models.CharField(max_length=50,blank=True,null=True)
imageNo_9 = models.CharField(max_length=50,blank=True,null=True)
imageNo_10 = models.CharField(max_length=50,blank=True,null=True)

img_desc_1 = models.CharField(max_length=500,blank=True,null=True)
img_desc_2 = models.CharField(max_length=500,blank=True,null=True)
img_desc_3 = models.CharField(max_length=500,blank=True,null=True)
img_desc_4 = models.CharField(max_length=500,blank=True,null=True)
img_desc_5 = models.CharField(max_length=500,blank=True,null=True)
img_desc_6 = models.CharField(max_length=500,blank=True,null=True)
img_desc_7 = models.CharField(max_length=500,blank=True,null=True)
img_desc_8 = models.CharField(max_length=500,blank=True,null=True)
img_desc_9 = models.CharField(max_length=500,blank=True,null=True)
img_desc_10 = models.CharField(max_length=500,blank=True,null=True)
.
.
.
img_alt_1 = models.CharField(max_length=500,blank=True,null=True)
img_alt_2 = models.CharField(max_length=500,blank=True,null=True)
img_alt_3 = models.CharField(max_length=500,blank=True,null=True)
img_alt_4 = models.CharField(max_length=500,blank=True,null=True)
img_alt_5 = models.CharField(max_length=500,blank=True,null=True)
img_alt_6 = models.CharField(max_length=500,blank=True,null=True)
img_alt_7 = models.CharField(max_length=500,blank=True,null=True)
img_alt_8 = models.CharField(max_length=500,blank=True,null=True)
img_alt_9 = models.CharField(max_length=500,blank=True,null=True)
img_alt_10 = models.CharField(max_length=500,blank=True,null=True)

python django-models django-forms model
1个回答
0
投票

我找到答案了:

对于范围 (1, 31) 中的第 30 行:

  locals()[f"imageNo_{rowthirty}"] = models.CharField(max_length=50,blank=True,null=True)
  locals()[f"img_desc_{rowthirty}"] = models.CharField(max_length=500,blank=True,null=True)
  locals()[f"img_alt_{rowthirty}"] = models.CharField(max_length=500,blank=True,null=True)

© www.soinside.com 2019 - 2024. All rights reserved.