Problems with submit button in change_form template Django

im new with Django and Python, and im still confuse about how to prepopulate values from manytomany field related lookup as my question in Prepopulate tabularinline with value from related lookup in manytomany field 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')

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

and here is my admin:

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

and here is my change_form override template:

{% extends "admin/change_form.html" %}
{% block after_field_sets %}{{ block.super }}

<form action="" method="post">
<input type="submit" name="enter" value="Enter" />
</form>
{% endblock %}

But, still nobody can tell me how :(. (If you please response to my question on that page). And now, im confusing about 2 problems: 1. I want my submit button in change_form to redirect to change_form too a.k.a in the same page need no refresh page (not to change_list page or the actual submission). 2. How can i get instances of related lookup 'produks' fieldset (manytomany) from submit button so that i can access parent values (Class Product) and prepopulate all to tabularinline (Class Foo or intermediary class)?

FYI, submit button is below all fieldsets.

Anyone help me please :(. Thank you for your kindly responses :).

Answers


your problem is, that this

<form action="" method="post">
<input type="submit" name="enter" value="Enter" />
</form>

will just send your submit-button back to your server. the form-tag needs to sorround every form-element.

just remove: <form action="" method="post"> and </form> and it may work.


as i understand you correctly you want to display an order (Order) and its items (Foo)?

my naive solution would be:

class Product(models.Model):
   ...

class Order(models.Model):
   products = models.ManyToManyField(Product, through='Item')

class Item(models.Model):
   order = models.ForeignKey(Order)
   product = models.ForeignKey(Product)
   quantity = models.IntegerField()
   ...

and the admin could be as simple as this:

class ItemInline(admin.TabularInline):
    model = Item

class OrderAdmin(admin.ModelAdmin):
    inlines = (ItemInline,)

you need to test that, as i cant (actually)


Need Your Help

Unix_timestamp in Apache Hive

java date time hive hiveql

I'm trying to convert a string date "Sat Jan 25 00:13:31 +0000 2014"

Memory leak after using free() in destroyer function in c (according to splint)

c memory splint

I am relearning C, and using splint to test my source code.