Django Query prefetch_related:'exceptions.NameError'对象不支持项目赋值

时间:2016-11-17 06:09:13

标签: django django-queryset

我有一对多关系表。这是我的模特

class Pointofsale(models.Model):
   official_receipt = models.CharField(max_length=250,blank=False,null=False)
   transaction_number = models.CharField(max_length=250,unique=True)
   company = models.ForeignKey("User_company")
   customer = models.ForeignKey("Customer")

class Pointofsale_details(models.Model):
   pointofsale = models.ForeignKey(Pointofsale, related_name='pointofsale_details')
   item = models.ForeignKey("Inventory")
   quantity = models.DecimalField(max_digits = 19, decimal_places = 6, default = 0.0) 

我想用这种格式的物品获得销售

  

{official_receipt:100,..等,pointofsale_details:[{item1},{item2}]}

OR(用于汇总报告)

  

[{official_receipt:100,..等,pointofsale_details:[{item1},{item2}]},{official_receipt:100,..等,pointofsale_details:[{item1},{item2}]}]

我为单个查询尝试了此查询,但无济于事:

transaction_details = Pointofsale.objects.filter(company=get_current_company(request)).prefetch_related('pointofsale_details')

我收到此错误

  

'exceptions.NameError'对象不支持项目分配

这是处理一对多关系的正确或正确方法吗? 我错过了什么?

如果不清楚,请随时在下面发表评论,以便我可以改进我的问题。谢谢!

干杯!

def get_current_company(request,company_obj = False):
    company = request.session.get('company_id',None)
    return company

参考文献:

0 个答案:

没有答案