0
The Quetion
你好。我試圖找出使用一種形式創建一個父對象然後創建0-n個子對象的最佳方式。我假設ModelForms是我需要做的,但我很難理解如何構建模板和視圖。有人願意解釋如何創建n個子對象嗎?django:如何使用動態0-n可能性的ModelForm?
研究
我讀過有關這幾個其他文章和帖子:
- django model/modelForm - How to get dynamic choices in choiceField?
- https://stackoverflow.com/questions/5575560/how-do-i-create-a-drop-down-menu-in-django-using-a-modelform-with-dynamic-values
- http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/
- Overriding the save method in Django ModelForm
僅舉幾例。
的詳細
我有這樣兩種模式,以便:
// models.py
class DataItem(models.Model):
name = models.CharField(max_length=255)
date_created = models.DateTimeField(auto_now_add=True)
date_last_updated = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, blank=False)
def __unicode__(self):
return self.name
class DataItemSet(models.Model):
item = models.ForeignKey(DataItem, blank=False)
type_ind = models.IntegerField()
而且我已經創建了兩個ModelForms對應:
//形式.py
class CreateDataItemForm(forms.ModelForm):
class Meta:
model = DataItem
exclude = ('owner',)
def save(self, user, commit=True):
item = super(CreateDataItemForm,self).save(commit=False)
item.owner = user
if commit:
item.save()
return item
class CreateDataItemSetForm(forms.ModelForm):
class Meta:
model = DataItemSet
exclude = ('item',)
def save(self, parent, commit=True):
set = super(CreateDataItemSetForm,self).save(commit=False)
set.item = parent
if commit:
set.save()
return set
在我看來,我試圖讓一個表單提交由DataItem
擁有的新DataItem
和1-n DataItemSet
的創建。這裏的形式的輸出的一個示例:
而模板:
<form action="." method="post">
{% csrf_token %}
<table>
{{ create_form.as_table }}
</table>
<table>
<tr>
<th>What to track:</th>
<td>
<select>
<option value="1">Number</option>
<option value="2">Currency ($)</option>
<option value="3">Date</option>
<option value="4">Day</option>
<option value="5">Time</option>
</select>
</td>
<td>
</td>
</tr>
<tr>
<th>What to track:</th>
<td>
<select>
<option value="1">Number</option>
<option value="2">Currency ($)</option>
<option value="3">Date</option>
<option value="4">Day</option>
<option value="5">Time</option>
</select>
</td>
<td>
<button type="button">+</button>
</td>
</tr>
</table>
<p>
<button type="submit">Create</button>
</p>
</form>
最後,該視圖:
// views.py
@login_required
@csrf_protect
def create_data_item(request):
create_form = CreateDataItemForm()
c = {'create_form':create_form}
c.update(csrf(request))
if request.method == 'POST':
data = request.POST.copy()
form = CreateDataItemForm(data, instance=DataItem())
item_sets = [CreateDataItemSetForm(request.POST, prefix=str(x), instance=DataItemSet()) for x in range(0,9)]
if form.is_valid():
# create new data item
new_item = form.save(request.user)
#create new set of stuff to track
for item_set in item_sets:
new_item_set = item_set.save(new_item)
# return to the add entry page
return redirect('/add')
else:
return render_to_response('internal/create_data_item.html',c)
我很想知道如何允許從t中動態提取0-n DataItemSets他form
。我無法弄清楚它與ModelForm。
感謝您的幫助!
我不理解的是「+」按鈕應該做些什麼來動態插入一個新的下拉列表(請參閱原始文章中的圖片)。研究FormSets/InlineForms時,我找不到任何文檔。你可以擴展嗎? – Kgosser 2012-03-24 22:17:22
我能夠找到這個,這真的幫助我!謝謝你的幫助。 https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform – Kgosser 2012-03-24 22:49:56