I have 3 models with various fields in each. For 2 of the models, I'm fine with using a generic form (through Django's create_object) to request data. I wrote a function that accepts the model name and sends the user to the generic form
url(r'^add_(?P<modelname>\w+)/$', generic_add),
def generic_add(request, modelname):
mdlnm_model = models.get_model('catalog',modelname)
return create_object(request,
model = mdlnm_model,
template_name = 'create.html',
post_save_redirect = '/library/',
extra_context = {'func': 'Create ' + modelname},
login_required = 'True'
)
For the 3rd model, I have a ModelForm class defined so that I can omit one of the fields in this model when the user sees the form.
url(r'^create_actor/$', create_object, Actor_Input, name='db_actor_create'),
Actor_Input = {
'form_class': ActorForm,
'template_name': 'create.html',
'post_save_redirect': '/library/',
'extra_context': {'func': 'Create Actor'},
'login_required': 'True'
}
class ActorForm(forms.ModelForm):
class Meta:
model = Actor
fields = ('name','age','height','short_description',
'long_description')
Is there a way for Django to display the defined ModelForm if it exists but otherwise display the fully generic form if a defined form has not been made? I anticipate creating many more models, and would rather not create a url for every single model that needs to be split out the way Actor is.
So put a different way, I want to alter the generic_add function so it will use the ActorForm (if it exists) but otherwise the generic ModelForm. I know how to check for the existance of the ActorForm class, but what if I want that to be dynamic as well? Something like checking if: modelname + 'Form' exists. I'm unsure how to dynamically send the user to a predefined form if one exists.
Any suggestions? Is there a better way to look at this problem?