django A表存储商品的ID和名称,B表存储商品的每时每刻价格信息 打开页面默认显示一个商品的价格信息,最下方有两个按钮 上一个 和 下一个。 点击可切换当前显示的商品价格信息

11浏览 · 2024-10-04 22:04:50
在Django中,要实现这种功能,可以采用模型(Model)与视图(View)的结合。假设我们已经定义了两个模型`Product`(对应于A表)和`PriceHistory`(对应于B表)。`Product`模型可能有`id`和`name`字段,而`PriceHistory`模型则包括`product_id`, `price`, 和 `timestamp`字段。 1. **创建模型**[^1]: ```python from django.db import models class Product(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=200) class PriceHistory(models.Model): product = models.ForeignKey(Product, on_delete=models.CASCADE) price = models.DecimalField(max_digits=10, decimal_places=2) timestamp = models.DateTimeField(auto_now_add=True)