WTForms/Flask-WTF class attributes vs instance attributes - python

I'm now trying to use Flask-WTF (powered by WTForms) to render my frontend forms submission.
However, there is a limitation that all the fields in a Form object are class-level attributes, namely all instances of the class share the same fields.
When I tried to add multiple form instances and render them on the same page, it just doesn't work because the __init__ function in a Form subclass doesn't work, you cannot add fields or modify fields in the __init__ function, and all the instances are sort of "synchronized" after I clicked the submit button.
I'm essentially trying to make a single submit button that submits several forms, while this built-in functionality of Form object hinders the process. I'm now trying to modify the BaseForm class which is inherited by Form, and see if I can dig out something to work :(
I believe this has something to do with the Metaclass in WTForms, if anyone has encountered this problem please save me, and many thanks...

Related

How can I reuse django form for searching?

I am programming an application in django, and I have a model where I defined some fields that are necessary to be filled. This way, when te user doesn't fill one of these fields, Django authomatically indicates to the user to fill it to create the specific object defined by the model.
But myquestion comes here: I want to reuse the same form to search objects defined by that model. And in this case, all the fields that before were necessary, now are OPTIONAL. But, as I have already defined the model so that the fields are necessary, django doesn´t let me define those fields as optional.
Is there any way to reuse that form where the fields are necessary, but making them OPTIONAL? Or I must create another different model or form in html? I know that creating another form manually in the html code the problem is solver, but I have curiosity to know if it can be reused.
Thank you so much!
You can programmatically change properties of a field within a form using its fields dictionary. So you could create a new form class that is derived from your current form class and in its __init__ set the required property of the fields you desired to be optional to be False like so:
self.fields['title'].required = False

How to customize Django inline admin forms based on the object instance

The Django documentation makes it clear how to customize ModelForm instances based on the attributes of the particular Model instance being edited. However, I am working with a design that involves a lot of foreign key relationships between models, and I keep running into situations where I would like to modify a particular inline form instance based on the inline Model associated with it. I have dug through the documentation and the code, but the solution for this is eluding me.
The closest thing to a hook for this that I've been able to find is InlineModelAdmin.get_formset(), but the object instance that gets passed to that method is the parent object, not an instance of the child object. My instinct is that there is a way to do this, though. Does anybody know the proper way?
I am not 100% sure I fully understand what you are asking, but you can specify a forms.ModelForm for the admin inline (https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.form) and that receives an instance of the current inline object and then you can change the form fields based on the instance.

Django: user profile creation forms

As I've understood the deal, the standard way of extending the User Django model is by implementing profiles.
This suits me wonderfully, since I have 3 types of profiles (3 different Model subclasses) all based upon the User model for uniqueness and authentication.
I'm having trouble deciding, however, on the best way of implementing a user creation form. Should I...
Use mixins: inherit a ModelForm with model=User and one with model=Profile, manually overriding save() and clean() to call the bases' methods and perform the foreign key assignment.
Use composition: inherit Form and manually instantiate and manage two ModelForms.
Instantiate the forms separately, and save() them in a manually ordered fashion.
The Best Way I Didn't Think Of.
I would appreciate a (however minimal) implementation so I can be sure I'm making the right calls.
Thanks in advance.
You can write a single form that acts as a combined ModelForm for both your Profile model and the contrib User model. To accomplish this, a little manipulation of the forms kwargs on init is required. See this snippet: http://djangosnippets.org/snippets/2081/

Django, userfriendly way to order objects in filter_horizontal widget in admin

I often use the filter_horizontal attribute for many-to-many relationships which renders a FilteredSelectMultiple widget.
The downside of this widget arises when the user needs to be able to order the objects.
This is not user friendly because the order can only be modified on the change_view of the object itself. Which may live in a complete different area of the admin.
Does anyone have ideas about how this can be improved? Is there a FilterSelectMultipleOrderable widget out there? (I didn't find it yet)
Update!!
We made a version which extends the default django widget.
https://github.com/fabrique/django-sortedm2m
This seems to do the job:
http://pypi.python.org/pypi?:action=display&name=django-sortedm2m
It just renders a list so it could use some styling.

how do you style a form already inside django

I'm using django-registration. I'd like to update the AuthenticationForm in 'django.contrib.auth.forms'. Specifically, I would like to pass the 'attrs' dict to add a few attributes. How do I update a form that comes with django?
You can use the views that come with auth and override the form parameter with your own form:
django.contrib.auth.views.login(request[, template_name, redirect_field_name, authentication_form])
More info here.
The standard way is to subclass AuthenticationForm, change the attrs in constructor, pass the form to login view and write a new entry in urls.py.
This is a nightmare: in order to add html attribute to a field it is necessary to use python subclassing, to know how exactly django's form metaclass work (self.fields['field'].widget.attrs, not just self.field.widget.attrs), to know regexes (for urls.py), to know how django's urls.py work (should you put the overriding line before of after include('django.contrib.auth.urls')?) and to know where is the auth form and auth view imported from.
And now the "commercial break": just use http://pypi.python.org/pypi/django-widget-tweaks for your task ;)

Categories