假设我在models.py
中有以下内容:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.PositiveIntegerField()
class Sales(models.Model):
customer = models.CharField(max_length=255)
date = models.DateField()
class SalesItem(models.Model):
sales = models.ForeignKey(Sales)
product = models.ForeignKey(Product)
quantity = models.PositiveIntegerField()
price = models.PositiveIntegerField()
我在admin.py
中使用django admin以及
from django.contrib import admin
from models import Product, Sales, SalesItem
class SalesItemInline(admin.TabularInline):
model = SalesItem
raw_id_fields = ('product',)
class SalesAdmin(admin.ModelAdmin):
inlines = (SalesItemInline,)
admin.site.register(Product)
admin.site.register(Sales, SalesAdmin)
为了填写新的销售项目表单,用户单击放大镜图标,选择产品,然后销售项目的产品字段将填充产品ID。除此之外,我希望价格字段自动填充所选的product.price值。
最实用的django方法是什么?