How to make my view better to save Django

Posted by user558251 on Stack Overflow See other posts from Stack Overflow or by user558251
Published on 2011-01-05T11:48:35Z Indexed on 2011/01/05 11:53 UTC
Read the original article Hit count: 188

Filed under:
|
|

Hy guys sorry for this post but i need help with my application, i need optimize my view. I have 5 models, how i can do this?

def save(request):

# get the request.POST in content
if request.POST:
    content = request.POST
    dicionario = {}
    # create a dict to get the values in content 
    for key,value in content.items():
        # get my fk  Course.objects
        if key == 'curso' :
            busca_curso  = Curso.objects.get(id=value)
            dicionario.update({key:busca_curso})
        else:
            dicionario.update({key:value})
    #create the new teacher
    Professor.objects.create(**dicionario)  

my questions are?

1 - How i can do this function in a generic way? Can I pass a variable in a %s to create and get? like this way ?

foo = "Teacher" , bar = "Course"

def save(request, bar, foo): if request post: ... if key == 'course' : get_course = (%s.objects.get=id=value) %bar ... (%s.objects.create(**dict)) %foo ???

i tried do this in my view but don't work =/, can somebody help me to make this work ? Thanks

© Stack Overflow or respective owner

Related posts about function

Related posts about save