views:

64

answers:

2

I have multiple models that I want to create generic inputs for. My first pass used two separate urls:

url(r'^create_actor/$, create_object, {'model': Actor, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Actor'}, 'login_required': 'True'}),

url(r'^create_movie/$, create_object, {'model': Movie, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Movie'}, 'login_required': 'True'}),

I assume it would be much better to combine these into one statement. I'm not sure how to pass a variable from the url into the parameters such that the line would dynamically select the model based on the variable.

A: 

I haven't tried this, but you can use a variable to capture the value after create_ and have it automatically sent to the create_object view:

url(r'url(r'^create_(?P<model>\w+)/$, create_object, {'template_name': 'create.html', 'post_save_redirect': '/library/', 'login_required': 'True'})

You'll have to access this url as /create_Actor/ instead of /creat_actor/. I'm not sure how to get the extra_context key to work. Hope this helps.

Luiz C.
Thanks for the reply. I tried something along those lines, but couldn't get it to work. I get the following error:'unicode' object has no attribute '__name__'
Ed
A: 
from django.db import models

url(r'^create_(?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/',
       login_required = 'True'
    )
Ed