0
我在我的應用中有不同的模型,主模型有一些其他模型的多個實例。在編輯視圖中預填充動態添加的表單集
models.py:
class Person(models.Model):
...
class Pet(models.Model):
owner = models.ForeignKey(Person)
...
forms.py:
class PersonForm(forms.ModelForm):
class Meta:
model = Person
PetFormSet = inlineformset_factory(Person, Pet, extra = 1)
views.py:
def add_template(request):
person_form = PersonForm(prefix = 'person_form')
pet_form = PetFormSet(instance = Person(), prefix = 'pet_form')
... # check is_valid(), render when no POST data is present, etc.
的要點是加入完美地工作,存儲的每個實例在相應的數據庫表等。我使用jquery.formset-1.2.js來管理動態添加刪除表單中的「add.html 」。
但我後來想通過視圖編輯存儲的信息,即從我傳入請求中的對象中加載數據,並使用從數據庫獲得的數據呈現窗體集(如果有3個寵物與被編輯的人,顯示3形成顯示其值的「寵物」的實例)。
我還想添加新的寵物並刪除現有的寵物,以及更改現有的字段值。
我已經嘗試使用FormSet創建中的查詢集,但它不顯示任何內容。
任何想法如何緩解這個問題?也許使用一個應用程序更容易的formset管理?
謝謝