且构网

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

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

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

可能最干净的事情是创建另一个产品表,并拥有多对多关系。 (见这里: 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).