Question

I am searching of a method to obtain html forms from some one-to-many relations, like order-lineorder, invoice-lineinvoice, etc.

Let me an example:

# models.py
class Order(models.Model):
    date = models.DateTimeField()
    number = models.IntegerField(default=0)

class LineOrder(models.Model):
    description = models.TextField()
    price = models.FloatField()
    order = models.ForeignKey(Order)

# views.py
def order_form(request):
    form = OrderForm()
    table_lineorder = LineOrderTable([])
    RequestConfig(request).configure(table)
    return render(request, "order_form.html", {"form": form, "table": table_lineorder})

Then, I want to obtain the order template with "generic attributes" (date, number), and a table list (originally empty) of lines order. Add some action like add, edit and remove must be possible.

I think that a solution like django-tables2 is possible, but I can't add rows dinamically, I think.

Thanks in advice.

[EDIT]

I have found the solution. It is django-dynamic-formset

No correct solution

OTHER TIPS

I'm not quite clear about your question, but I guess this might be what you want:

class Order(models.Model):
    date = models.DateTimeField()
    number = models.IntegerField(default=0)
    items = model.ManyToManyField(Item)

class Item(models.Model):
    description = models.TextField()
    price = models.FloatField()

It should be equivalent to one-to-many if you don't assign one Item to multiple Orders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top