class BookSerializer(serializers.ModelSerializer):
class Meta:
fields = ('title',)
model = Book
class BookUpdateView(vanilla.UpdateView):
form_class = BookSerializer
success_url = reverse_lazy('books:list')
template_name = 'books/update.html'
{% with field = form.title.as_form_field() %}
<label for="{{ field.name }}">{{ field.label }}</label>
<input id="{{ field.name }}" name="{{ field.name }}" value="{{ field.value }}">
{% if field.errors %}
{% for error in field.errors %}
<span class="error">{{ error }}</span>
{% endfor %}
{% endif %}
{% endwith %}
def get_form(self, data=None, files=None, **kwargs):
cls = self.get_form_class()
return cls(data=data, files=files, **kwargs)
def get_form(self, data=empty, files=None, **kwargs):
cls = self.get_form_class()
return cls(data=data, **kwargs)
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = ('name',)
AuthorFormSet = modelformset_factory(Author, form=AuthorForm)
formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O'))
class BookSerializer(serializers.ModelSerializer):
class Meta:
fields = ('title',)
model = Book
class AuthorSerializer(serializers.ModelSerializer):
books = BookSerializer(many=True)
class Meta:
fields = ('name', 'books',)
model = Author
This is just a rough pass (on my phone) feel free to expand in any of these that I've not done a good job on and can discuss further...
1) would be completely reasonable, yup. Vanilla views wouldn't quite be ideal as-is because it doesn't inherit for rest frameworks view class. However it's a simple project so it'd be easy enough to create an equivalent DRF specific project. (And yup the serializer init arguments would be slightly different as you point out)
The generic views provided by DRF are also not quite suitable as they use API conventions rather than browser conventions and have a slightly diff set of behaviour.
2) don't bother replicating modelformset_factory. I deliberately removed the .model shortcut to force users to explicitly specify the serializer class and queryset and reduce the amount of magic and indirection. I think it's presence is slightly poor API, and removing it from a GCBV implementation ends up with super simple behaviour.
To render a serializer instance in a template you'll need to have a go at wrapping HTMLFormRenderer in a template tag.
3) again, don't bother replicating inlineformset-somethingsomething-factory. Either ensure that the serializer is called with many=True or enforce that an explicit ListSerializer is used for those cases.
The template rendering there similar to before but relys on the ticket you've referenced. There's some support there ATM but it's not documented or well used/tested yet.
Cheers,
Tom