Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

django inline formsets with a complex model for the nested form

Say I have django model that looks something like this:

class Order(models.Model):
 number = models...
 date = models...

class OrderLine(models.Model):
 # One or more lines per order
 order = models.ForeginKey(Order)
 common_line = models.OneToOneField(CommonLine)

class CommonLine(models.Model):
 # common elements of what might be on a line item...
 taxes = model...
 amount = model...

I want to create a form that uses an inlineformset to edit one or more Lines (both OrderLine and CommonLine) per order.

I can create a formset that works with Order and OrderLine - but how do I get the inline formset to give me all the detailed items from the CommonLine class when displaying the formset. It seems the documentation on inline formsets requires that the inline form - the multiple lines on an order can only map to a single class...

Am I not seeing something in the documentation? I'm sure I can probably override something, I'm just not sure where.

Thanks for any help...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved problem with http://yergler.net/blog/2009/09/27/nested-formsets-with-django/. Pleas use the following correction in forms.py file:

        instance=None
            pk_value = hash(form.prefix)

+       correct_data = None
+       if (self.data):
+           correct_data = self.data;
        # store the formset in the .nested property
        form.nested = [
-           TenantFormset(data=self.data,
+           TenantFormset(data=correct_data,
                            instance = instance,

Just working on Django 1.4.1 very well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...