开发者

Using a custom form in a modelformset factory?

开发者 https://www.devze.com 2022-12-31 15:23 出处:网络
I\'d like to be able to use a customized form in a modelformset_factory. For example: models.py class Author(models.Model):

I'd like to be able to use a customized form in a modelformset_factory. For example:

models.py

class Author(models.Model):
    name = models.CharField()
    address = models.CharField()

class AuthorForm(ModelForm):
   class Meta:
        model = Author

views.py

def test_render(request):
    myModelFormset = modelformset_factory(Author)
    items = Author.objects.all()
    formsetInstance = myModelFormset(queryset = items)
    return render_to_response('template',开发者_运维百科locals())

The above code works just fine, but note I'm NOT using AuthorForm. The question is how can I get the modelformset_factory to use the AuthorForm (which I plan to customize later) instead of making a default Author form?


I think you should be able to pass the custom model form like so:

myModelFormset = modelformset_factory(Author, form=AuthorForm)
0

精彩评论

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