在multtomany字段中从相关查找中获取值的预填表 [英] Prepopulate tabularinline with value from related lookup in manytomany field

查看:180
本文介绍了在multtomany字段中从相关查找中获取值的预填表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题要问。我想让我的 change_form 从预期的相关查询的值(使用manytomany字段)预填表格线。所以当我在课程订单中的 change_form 中搜索 kode_prod 我可以得到它的实例,并在我的表格线(class Foo )中预填充。



这里是我的模型



  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 = ('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 = uKode Produk)
no_customer = models.ForeignKey(Customer,null = True,blank = True ,related_name ='%(class)s_kode_cust')


class Foo(models.Model):
product = model s.ForeignKey(Product,editable = False)
pemesanan = models.ForeignKey(Order)
quantity = models.IntegerField()
price = models.IntegerField()
discount = models.IntegerField()
tax = models.IntegerField()

Class 订单是与 Product 的manytomanyfield关系,所以在 change_form 中我可以得到它的相关查询的实例。 Class Foo change_form 中的表格线的中介。那么,有没有办法让我这样做?请帮助我,谢谢你的回复:)。

解决方案

你应该编写一个jQuery脚本,当一个 kode_prod ,当页面加载或点击添加另一个Foo链接时。您甚至不需要自定义提交按钮,因为您可以为'blur'事件注册一个事件监听器。



您的脚本发送一个异步XMLHttpRequest,该对象由django视图回答:

  from django.core import serializers 

def get_product(request,pk):
return HttpResponse(serializers.serialize(xml,Product.objects.filter(pk = pk)))

然后在响应句柄中的jQuery脚本中,从响应中填充所有需要的字段。



下一次请在您的问题上更具体和明确。


I have question to ask. I want to have my change_form to prepopulate the tabularinline from intances' value of related looks up (use manytomany field). So when I search kode_prod in change_form in class Order, I could get its instance and prepopulate all in my tabularinline (class Foo).

here is my model

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')


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 Order is manytomanyfield relationship to Product, so that in change_form I can get its instance with related looksup. Class Foo is intermediary that comes to tabularinline in change_form. So, are there some ways for me to do that ? Please help me and thank you for your kindly responses :).

解决方案

You should probably write a jQuery script that is called when a kode_prod is entered, when the page loads or when the "Add another Foo" link is clicked. You don't even need your custom submit button, since you can register an event listener for the 'blur' event.

You script sends an asynchronous XMLHttpRequest which is answered by a django view like that:

from django.core import serializers

def get_product(request, pk):
    return HttpResponse(serializers.serialize("xml", Product.objects.filter(pk=pk)))

Then in your jQuery script in the response handler populate all needed fields from the response.

Next time please be a lot more specific and clear in your question.

这篇关于在multtomany字段中从相关查找中获取值的预填表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆