假设我有以下内容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()
我在 django admin 中使用以下内容admin.py
:
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 方法是什么?