Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

M\nDidn&amp;#x2019;t you just show that fields are stored in a dict in the form? Why not just add another field that way?\n

M\nDidn&amp;#x2019;t you just show that fields are stored in a dict in the form? Why not just add another field that way?\n

D\n\n

M - Let&apos;s override form constructor to wrap the logic of adding fields on the go\n\n

M - Let&apos;s override form constructor to wrap the logic of adding fields on the go\n\n

D\n\n

M\n

M\n

M\n\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n

D\n\n

M\n

M\n

M\n

D\n\n

M\n\n

M\nBecause both DUF and DFF do things in a clean, non-magical way - they just work together. \nNamespaces and clean code beat smart hackery any day.\n

M\nTell me what is coming down the road\n

D\n

D\n

D\n

D\nIt&amp;#x2019;s okay that there are different objects\n

D\nIt&amp;#x2019;s okay that there are different objects\n

D\nIt&amp;#x2019;s okay that there are different objects\n

M\n\n

D\n\n

D\n

D\n\n

M\n

M Of course you could do the same with a regular CharField using form validationclean_&lt;fieldname&gt; method, but...\n

M - Duplicated code. So do a custom form field instead of duplicating code everywhere\nYes, you could solve this with form inheritance, but what if the inheritance chain gets too long?\n

M\n\n

D This field returns an already JSON validated Python list when accessing cleaned_data\n

D\n\n

M We are using MultiWidget.\n

M We are using MultiWidget.\n

M The difference is that you can get fancier with validation on the list than the string\n

D\n\n

M You need to implement compress\nIt would be nice that fields were also an attribute in the parent class.\n

M You need to implement compress\nIt would be nice that fields were also an attribute in the parent class.\n

M You need to implement compress\nIt would be nice that fields were also an attribute in the parent class.\n

M You need to implement compress\nIt would be nice that fields were also an attribute in the parent class.\n

M\n

M\n\n

D - You are going to love how easy this is! Ready?\n

D - You are going to love how easy this is! Ready?\n

D\n\n

M\n

M\n\n

M\n

M\n

M\n

M * You can raise a ValidationError with a code. * You can raise a ValidationError with a list of messages. * You could raise a ValidationError on the first email that fails, but error will be less helpful. * Hard to customize, no error_messages, Do this in an upper layer, rewrite run_validators\n

5.
Tons of technical content• We probably won’t have time for questions Advanced Django Form Usage @pydanny / @maraujop 4

6.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk Advanced Django Form Usage @pydanny / @maraujop 4

7.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk• Too much content in the abstract Advanced Django Form Usage @pydanny / @maraujop 4

8.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk• Too much content in the abstract• Special thanks to: Advanced Django Form Usage @pydanny / @maraujop 4

9.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk• Too much content in the abstract• Special thanks to: • Brian Rosner Advanced Django Form Usage @pydanny / @maraujop 4

10.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk• Too much content in the abstract• Special thanks to: • Brian Rosner Advanced Django Form Usage • James Tauber @pydanny / @maraujop 4

11.
Tons of technical content• We probably won’t have time for questions• Slides will be posted right after the talk• Too much content in the abstract• Special thanks to: • Brian Rosner Advanced Django Form Usage • James Tauber @pydanny / @maraujop • Frank Wiles 4

76.
I can make it smaller!def my_model_tiny_add(request,template_name=myapp/my_model_form.html): form = MyModelForm(request.POST or None) if form.is_valid(): form.save() Not setting defaults here return redirect(home) return render(request,template_name,{form:form,mymodel:mymodel}) Advanced Django Form Usage Good practice: Use model ﬁeld defaults rather @pydanny / @maraujop than doing it in your views. 26

104.
formsets.pyclass ItemFormSet(BaseFormSet): def __init__(self, numberItems, *args, **kwargs): super(ItemFormSet, self).__init__(*args, **kwargs) self.numberItems = numberItems def clean(self): # Dont bother validating the formset # unless each form is valid on its own if any(self.errors): return for form in self.forms: Advanced Django Form Usage if not form.cleaned_data[your_choice] == mod_wsgi: @pydanny / @maraujop raise ValidationError(umod_wsgi is the way to go!) 42

105.
formsets.pyclass ItemFormSet(BaseFormSet): def __init__(self, numberItems, *args, **kwargs): super(ItemFormSet, self).__init__(*args, **kwargs) self.numberItems = numberItems def clean(self): Forms must have # Dont bother validating the formset ‘your_choice’ ﬁeld that # unless each form is valid on its own if any(self.errors): return only accepts “mod_wsgi” for form in self.forms: Advanced Django Form Usage if not form.cleaned_data[your_choice] == mod_wsgi: @pydanny / @maraujop raise ValidationError(umod_wsgi is the way to go!) 42

137.
The docs on custom ﬁelds“Its only requirements are that it implementa clean() method and that its __init__()method accept the core argumentsmentioned above (required, label, initial,widget, help_text).” Advanced Django Form Usage @pydanny / @maraujop 62

138.
The docs on custom ﬁelds“Its only requirements are that it implementa clean() method and that its __init__()method accept the core argumentsmentioned above (required, label, initial,widget, help_text).” Advanced Django Form Usage The docs seem to be wrong @pydanny / @maraujop 62