Django multiple form factory

2024/11/18 15:22:18

What is the best way to deal with multiple forms? I want to combine several forms into one. For example, I want to combine ImangeFormSet and EntryForm into one form:

class ImageForm(forms.Form):image = forms.ImageField()
ImageFormSet = formset_factory(ImageForm)class EntryForm(forms.Form):title = forms.CharField(max_length=100)result_form = combine(EntryForm, ImageFormSet) # here it goes

I found 2 years old presentation introducing multipleform_factory() method, but I am not sure that it's the best way: http://www.slideshare.net/kingkilr/forms-getting-your-moneys-worth

Answer

An idea (not checked if it works):

class MySuperForm(CombinedForm):includes = (ImageForm, EntryForm, )

You see here how the form is built. You can make your own Form by extending from BaseForm and supplying another __metaclass__.

class CombinedForm(BaseForm):__metaclass__ = DeclarativeFieldsMetaclassFromMultipleClasses

In DeclarativeFieldsMetaclassFromMultipleClasses you do basically the same as here, except you sum up the declared fields from the classes on

class DeclarativeFieldsMetaclassFromMultipleClasses(type):def __new__(cls, name, bases, attrs):for clazz in attrs['includes']:attrs['base_fields'] += get_declared_fields(bases, clazz.attrs)new_class = super(DeclarativeFieldsMetaclassFromMultipleClasses,cls).__new__(cls, name, bases, attrs)if 'media' not in attrs:new_class.media = media_property(new_class)return new_class
https://en.xdnf.cn/q/71047.html

Related Q&A

How to include the private key in paramiko after fetching from string?

I am working with paramiko, I have generated my private key and tried it which was fine. Now I am working with Django based application where I have already copied the private key in database.I saved m…

SHA 512 crypt output written with Python code is different from mkpasswd

Running mkpasswd -m sha-512 -S salt1234 password results in the following:$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81I have this snippet of Python…

Running python scripts in Anaconda environment through Windows cmd

I have the following goal: I have a python script, which should be running in my custom Anaconda environment. And this process needs to be automatizated. The first thing Ive tried was to create an .exe…

How to work out ComplexWarning: Casting complex values to real discards the imaginary part?

I would like to use a matrix with complex entries to construct a new matrix, but it gives me the warning "ComplexWarning: Casting complex values to real discards the imaginary part".As a resu…

Is it possible to use POD(plain old documentation) with Python?

I was wondering if it is possible to use POD(plain old documentation) with Python? And how should I do it?

ctypes error AttributeError symbol not found, OS X 10.7.5

I have a simple test function on C++:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h>char fun() {printf( "%i", 1…

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:> time_deltas499900 -1 days +23:45:13 499916 -1 days +23:50:57 499917 00:03:2…

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…