Monday, July 1, 2013

Re: Get Form from string?

Hey MacVictor,

So when I needed to do something similar, I landed up hard-coding a dict with form-name -> form-class in my model definition. Honestly, there is probably a better way to do it, but that worked fine for my use-case, though it did feel slightly "not right". Any-how, fwiw, I did something like:

from forms import FormA, FormB, FormC
class Foo(models.Model):
  ...
  forms = {
    'SomeFormA': FormA,
    'SomeFormB': FormB,
    'SomeFormC': FormC
  }

And then in your view, something like:
form = model.forms[<form_name>] .. etc..

I'm guessing from your email that what you need is something similar, and perhaps someone from the list has a better idea than the above to do it ..

Hope that helps, though ..

All the best,
Sanjay


On Sun, Jun 30, 2013 at 1:59 AM, MacVictor <witold.gren@gmail.com> wrote:
I try in view get the form by name.
Example:
this is my url: 

/some-url/MyForm/

url(r'^/some-url/(?P<form_name>[a-zA-Z0-9]+)/', 'my_view', name='my-wiev'),


and my view:

def my_view(request, form_name):
    form = how_get_form_name(form_name)
    response = {}
    response['my_form'] = form
    return render_to_response('request/window.html', response)


I have create uniwersal view, get my form and send to template. Why form? Because my model has more Forms and I want to select which forms.



W dniu piątek, 28 czerwca 2013 23:06:15 UTC+2 użytkownik yeswanth nadella napisał:
I am afraid, I did not understand your question. Are you trying to generate a form based on your model/ models? 

On Friday, June 28, 2013 3:23:00 PM UTC-5, MacVictor wrote:
and how to get only Form (not ModelForm) use string name?

W dniu piątek, 28 czerwca 2013 22:09:41 UTC+2 użytkownik MacVictor napisał:
get the model by string i use:

from django.db.models.loading import get_model
def get_model(self, app_label, model_name, seed_cache=True):

how to get ModelForm by string?

i try used:

modelforms = forms.ModelForm.__subclasses__()

def get_form(form):
    try:
        for model_form in modelforms:
            try:
                if model_form.Meta.form == form:
                    return model_form
            except AttributeError:
                continue
    except IndexError:
        print "Does not exist Model Forms for the object"

but I must import the form in which I will seek appropriate by name!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment