且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Django 数据库模型的字段中存储字典

更新时间:2023-02-24 09:07:32

可能最简洁的做法是创建另一个Products"表并建立多对多关系.(参见此处:https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships .在文档中,他们使用了一个有很多配料的披萨的例子.)

Probably the cleanest thing to do would be to create another "Products" table and have a many-to-many relationship. (See here: https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships . In the docs they use the example of a pizza having many toppings.)

另一种选择是序列化您的 bill_products.在这种情况下,您可以执行以下操作:

The other option would be to serialize your bill_products. In that case, you'd do something like:

bill_products = json.dumps([rand_products])

这将在 for 循环之外(尽管在上面的示例中,rand_products 只是一个值,因此您需要修复它).

This would be outside of the for loop (although, in your example above, rand_products is only a single value, so you'll need to fix that).