为了账号安全,请及时绑定邮箱和手机立即绑定

对象列表中转换的 QuerySet 数据转换为 Tuple 中的 str/int 值

对象列表中转换的 QuerySet 数据转换为 Tuple 中的 str/int 值

潇湘沐 2023-08-03 17:10:39
我在非常简单的操作中遇到了非常奇怪的问题。models.pyNbProducts(models.Model):    brand = models.CharField(max_length=45, blank=True, null=True)    name = models.CharField(max_length=45, blank=True, null=True)    cluster = models.CharField(max_length=45, blank=True, null=True)    target_market = models.CharField(max_length=10, blank=True, null=True)    cpu_vendor = models.CharField(max_length=45, blank=True, null=True)    base_platform = models.CharField(max_length=45, blank=True, null=True)    gpu_list = models.CharField(max_length=45, blank=True, null=True)    screen_size = models.CharField(max_length=45, blank=True, null=True)    screen_resulution_list = models.CharField(max_length=45, blank=True, null=True)    touchscreen = models.CharField(max_length=45, blank=True, null=True)views.py    list_Products = NbProducts.objects.\                    filter(id__in=products_for_execute).\                    values('brand', 'name', 'id')# list_Products:#<QuerySet [{'brand': 'Acer', 'name': 'Aspire R7-372T', 'id': 2713}, #{'brand': 'Acer', 'name': 'Aspire S7-393', 'id': 2716}, #{'brand': 'Acer', 'name': 'Swift SF514-51', 'id': 2743},....    class FProducts(object):        def __init__(self, id, brand, name):            self.id = str(id),            self.brand = str(brand),            self.name = str(name)           print(self.id, self.brand, self.name)        fproducts = list()    for i in list(list_Products):        fproducts.append(FProducts(id=i['id'], brand=i['brand'], name=i['name']))>> {'2713',) ('Acer',) 'Aspire R7-372T'>> {'2716',) ('Acer',) 'Aspire S7-393'>> {'2743',) ('Acer',) 'Swift SF514-51'所以。没有任何命令,它会将两个参数 - 'id' 和 'brand' 放入元组中。参数“名称” - 好吧,只是字符串。我不t need Tuple. And I don明白有什么关系。另外我把这个放进去。  self.id = self.id[0]  self.brand = self.brand[0]好的,有帮助,应用程序可以工作。但我看不到问题的根源。这是Python(3.7)还是Django(2.1.7)的问题?
查看完整描述

1 回答

?
茅侃侃

TA贡献1842条经验 获得超21个赞

您自己在这里创建了一个元组:

        self.id = str(id),
        self.brand = str(brand),
        self.name = str(name)

应该:

        self.id = str(id)
        self.brand = str(brand)
        self.name = str(name)


查看完整回答
反对 回复 2023-08-03
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信