问题
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 :).
回答1:
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.
回答2:
do you need an relationship between Order
and Product
through Foo
?
then django-documentation might help you
来源:https://stackoverflow.com/questions/10975362/prepopulate-tabularinline-with-value-from-related-lookup-in-manytomany-field