基于django中的表单输入和字符串操作进行查询

时间:2018-06-14 04:42:34

标签: django database

Django fields in form

正如问题4所述,我需要获取表单输入并计算上传的每个图像的design-id。如何查询数据库以获取类别,结构,制造商和制造商位置的缩写,并连接所有这些字符串以生成和存储设计ID?

我在views.py

中添加了此代码
image = form.save(commit=False)

cat = Category.objects.get(name=image['category'])      
fab = Fabric.objects.get(name=image['fabric'])
manu = Manufacturer.objects.get(name=image['manufacturer'])
setcat = Group.objects.get(name=image['set_cat'])
string = cat.abbr + "-" + fab.abbr + "-" + manu.name_abbr + "-" + manu.loc_abbr + "-" + image['selling_price'] + "-" + setcat.abbr + "-" + image['set_cat_no']
image['design_id'] = string

image.save()

这给了我错误 - '图片'对象没有属性' getitem '

models.py中的我的图片模型就是这个

class Images(models.Model):
    design_id = models.CharField(max_length=128)
    file = models.ImageField(upload_to='images')
    cost_price = models.FloatField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=False)
    fabric = models.ForeignKey(Fabric, on_delete=models.CASCADE, blank=False)
    manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE, blank=False)
    selling_price = models.FloatField()
    aliveness = models.IntegerField(default=1)
    date_added = models.DateTimeField(default=datetime.datetime.now)
    set_cat = models.ForeignKey(Group, on_delete=models.CASCADE, blank=False)
    set_cat_no = models.IntegerField()
    set_cat_name = models.CharField(max_length=50, blank=True)
    visit_count = models.IntegerField(default=0)

我尝试从__unicode__返回unicode(),但这也没有用。

1 个答案:

答案 0 :(得分:1)

您无法通过字典表示法访问模型实例属性。你需要使用点符号,就像你在其他地方所做的一样。

image.design_id = string