开发者

Django "more dyanmic" forms

开发者 https://www.devze.com 2023-02-26 11:04 出处:网络
I have a question about \"more dynamic\" django forms. say, i have a webpage which represents Account with a list of users, soeach account will have 1 or more users, there is some javascript codeto g

I have a question about "more dynamic" django forms.

say, i have a webpage which represents Account with a list of users, so each account will have 1 or more users, there is some javascript code to generate extra row to allow user data gets entered.

i think this is pretty common use case, but not sure how to make it happen, or simply, i shouldn't worry ab开发者_开发知识库out using django forms in this case?

Thanks


You should use a formset for that. Specifically, you can use the Inline formset factory to achieve the same.

From the documentation of that,

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=100)

>>> from django.forms.models import inlineformset_factory
>>> BookFormSet = inlineformset_factory(Author, Book)
>>> author = Author.objects.get(name=u'Mike Royko')
>>> formset = BookFormSet(instance=author)

If you are using the django-admin, you can display these inlines with the javascript to add a new entry, by using the InlineModelAdmin in either the stacked way, or tabular way.

0

精彩评论

暂无评论...
验证码 换一张
取 消