我需要将字典保存在模型的字段中。我怎么做?如何在Django数据库模型的字段中存储字典
例如,我有这样的代码:
def create_random_bill(self):
name_chars = re.compile("[a-zA-Z0-9 -_]")
bill_name = "".join(random.choice(name_chars for x in range(10)))
rand_products = random.randint(1,100)
for x in rand_products:
bill_products =
new_bill = Bill.new(name=bill_name, date=datetime.date, products=bill_products)
new_bill.save()
我怎么写“bill_products =”这样可以节省一些随机的产品,从我的产品型号,以这一法案?
这是该法案的模型描述:
class Bill(models.Model):
name = models.CharField(max_length=255)
date = models.DateTimeField(auto_now_add=True)
products = models.ManyToManyField(Product, related_name="bills")
而且也是该产品的型号说明:
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.IntegerField()
如果有什么我要补充只是发表评论。谢谢!
工作到底是什么“蟒蛇数据库模式”?你在使用特定的ORM还是框架?这看起来有点'django-ish' – SingleNegationElimination 2012-03-13 14:58:17
是的,它是Django。我刚刚开始使用它,所以我可能会混淆“Python”与“Django”。我会改变标题。 – 2012-03-13 15:02:52