我有一个名为vehicle的模型。
var o = {}; // new empty Object that will be used for next payload
msg.payload.split(',').forEach(function(kv) { // loop on 'key=value' entries in array
var pair = kv.split('='); // split entry to isolate key and value
var key = pair[0];
var value = pair[1];
o[key] = value; // save that as new property for our Object
});
msg.payload = o; // Make that object the new payload
以下是存储的样本数据。
class Vehicle(models.Model):
name = models.CharField(max_length=200)
price = models.models.DecimalField(max_digits=19, decimal_places=2)
现在我想根据订单制作发票。有人订购了2个循环,1个公共汽车和3个汽车。
我想要一个名为Invoice的模型,该模型将包含2个循环,1个总线和3个汽车。在它。
并在最后在浏览器中创建发票,如下所示:
id name price
1 cycle 100
2 bus 10000
3 car 1000
如何创建模型:
id vehicles no_of_ordered unit_price total_price
1 cycles 2 100 200
2 bus 1 1000 1000
3 car 3 10000 30000
之后如何使用上述模型创建发票清单
答案 0 :(得分:0)
我建议您创建两个代表发票和发票头寸的模型。 Invoice
模型将保存一般信息(例如日期或相应的用户),InvoicePosition
模型将保存Vehicle
的实例和订购数量。您还可以添加单价,以便在更改车辆价格后,您之前的发票不会出错。
以下是我在代码中的建议:
class Invoice(models.Model):
pass
class InvoicePosition(models.Model):
invoice = models.ForeignKey(Invoice, related_name='invoice_positions', on_delete=models.CASCADE)
vehicle = models.ForeignKey(Vehicle, related_name='vehicle_positions', on_delete=models.CASCADE)
quantity = models.PositiveSmallIntegerField()
# You could use the price field to cache the price when the invoice was issued.
price = models.models.DecimalField(max_digits=19, decimal_places=2)
以下是创建车辆和发票的完整示例,例如:
cycle = Vehicle.objects.create(name='cycle', price=100)
bus = Vehicle.objects.create(name='bus', price=10000)
car = Vehicle.objects.create(name='car', price=1000)
invoice = Invoice.objects.create()
invoice.invoice_positions.create(vehicle=cycle, quantity=2, price=cycle.price)
invoice.invoice_positions.create(vehicle=bus, quantity=1, price=bus.price)
invoice.invoice_positions.create(vehicle=car, quantity=3, price=car.price)