Python Django:合并表单和数据库中的文本
我目前正在与一个Django项目合作 我有字典在里面: 来自models.pyPython Django:合并表单和数据库中的文本,python,django,postgresql,Python,Django,Postgresql,我目前正在与一个Django项目合作 我有字典在里面: 来自models.py class teltab(models.Model): code=models.CharField(max_length=255) telescope=models.CharField(max_length=255) comment=models.TextField(blank=True) 以及向字典添加数据的表单: class newtelescopesform(forms.ModelF
class teltab(models.Model):
code=models.CharField(max_length=255)
telescope=models.CharField(max_length=255)
comment=models.TextField(blank=True)
以及向字典添加数据的表单:
class newtelescopesform(forms.ModelForm):
class Meta:
model=teltab
现在我需要做以下工作:
- 从数据库中获取文本(Сomment 1)(我正在运行Postgresql)
- 从表格中获取文本(Сomment 2)
- 组合数据(С元素1+С元素2)
- 最后将结果写入数据库
if len(request.GET['comment'])>0:
commentq=request.GET['comment'] # I expect that it will take the text from form
p=tel_list.get('comment', "") # I expect that it will take the text from database
commentp = p + "\n" + commentq # and it should merge the text
tel_list.update(comment=commentq)
for item in tel_list:
item.save()
但它给了我一条错误消息:“太多的值无法解压缩”
我的问题是我做错了什么?错误的原因是什么?
请教我一个解决这个问题的好办法
(很抱歉这么长的文字,我显然不知道我在做什么。)您几乎可以肯定,最好使用单独的
注释
模型,并与原始模型建立外键关系
class Teltab(models.Model): # it’s good python coding style to capitalize classnames
code = models.CharField(max_length=255)
telescope = models.CharField(max_length=255)
class Comment(models.Model):
text = models.TextField(required=True)
teltab = models.ForeignKey(Teltab)
然后,您可以获得有关原始模型的所有评论,例如-
my_teltab = Teltab.objects.first()
my_teltab.comment_set.all()
几乎可以肯定,使用单独的
注释模型并与原始模型建立外键关系会更好
class Teltab(models.Model): # it’s good python coding style to capitalize classnames
code = models.CharField(max_length=255)
telescope = models.CharField(max_length=255)
class Comment(models.Model):
text = models.TextField(required=True)
teltab = models.ForeignKey(Teltab)
然后,您可以获得有关原始模型的所有评论,例如-
my_teltab = Teltab.objects.first()
my_teltab.comment_set.all()
您已经定义了一个表单,为什么不使用表单而不是使用请求。GET
?在执行过程中它在哪里给您带来错误?您已经定义了一个表单,为什么不使用表单而不是使用请求。GET
?在执行过程中它在哪里给您带来错误?