【发布时间】:2012-06-11 19:25:50
【问题描述】:
我是 Django 和 Python 的新手,我仍然对如何从与多字段相关的查找中预填充值感到困惑,这是我在 Prepopulate tabularinline with value from related lookup in manytomany field 中的问题 这是我的模型:
class Product(models.Model):
product_name= models.CharField(max_length=50)
price = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
tax_per_item = models.DecimalField(max_digits=10, null=True, blank=True, decimal_places=2, default=Decimal('0.00'))
discount_per_item = models.DecimalField(max_digits=10, null=True, blank=True, decimal_places=2, default=Decimal('0.00'))
class Order(models.Model):
produks = models.ManyToManyField(Product, verbose_name=u"Kode Produk")
no_customer = models.ForeignKey(Customer, null=True, blank=True, related_name='%(class)s_kode_cust')
def order_view(request):
if 'enter' in request.POST:
#response to tabular.html template
return HttpResponseRedirect('/admin/POS/Pemesanan/inline')
class Foo(models.Model):
product = models.ForeignKey(Product, editable=False)
pemesanan = models.ForeignKey(Order)
quantity = models.IntegerField()
price = models.IntegerField()
discount = models.IntegerField()
tax = models.IntegerField()
这是我的管理员:
class PemesananAdmin(admin.ModelAdmin):
fieldsets = (
('Customer in Time (Person)', {
'fields': ('no_customer',),
}),
('Date', {
'fields' : ('date', 'delivery_date',),
}),
('Order Details', {
'fields' : ('produks',),
}),
)
search_fields = ['produks', 'no_customer']
raw_id_fields = ('produks', 'no_customer',)
related_lookup_fields = {
'fk': ['no_customer'],
'm2m': ['produks'],
}
inlines = [
FooInline,
]
class FooInline(admin.TabularInline):
model = Foo
template = 'admin/POS/Pemesanan/inline/tabular.html'
extra = 0
allow_add = True
这是我的 change_form 覆盖模板:
{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}
<form action="" method="post">
<input type="submit" name="enter" value="Enter" />
</form>
{% endblock %}
但是,仍然没有人能告诉我如何 :(。(如果您请在该页面上回答我的问题)。现在,我对 2 个问题感到困惑: 1. 我希望我在 change_form 中的提交按钮也重定向到 change_form ,也就是在同一页面中不需要刷新页面(而不是 change_list 页面或实际提交)。 2. 如何从提交按钮获取相关查找'produks' 字段集(manytomany)的实例,以便我可以访问父值(产品类)并将所有内容预填充到表格行(Foo 类或中间类)?
仅供参考,提交按钮位于所有字段集下方。
请任何人帮助我:(。感谢您的友好回复:)。
【问题讨论】:
标签: python django many-to-many submit