Re: passing foreign key via post
On 6/10/07, Car <[EMAIL PROTECTED]> wrote: > > > > Why not create it there? That is the moment you have all the data you > > need and all you're doing is calling a constructor. The code you've > > written there is a pretty normal pattern for taking form information and > > putting it into a model. > > Because it forces me to make different view for each model. > Its very difficult for novice to understand which logic should be > placed in views, which on models. > Is there a resource with django-patterns or good programming > practices ? What are the chances of those different views only containing minor variations? If you find yourself duplicating (copy-paste) parts of a view between views, then it's probably better for you to move oft duplicated code into a function. If you don't like putting Model instantiation and initialization in your views, you can create a static method for each Model. For example, see: http://dpaste.com/11939/ which is based on your original code. As for programming practices, look for the book "Python Programming Patterns" by Thomas W. Christopher published by Prentice-Hall. I must caution you though that most of the material there assumes at least programming experience and at least some knowledge of Python. For online resources, I find myself coming back to the "Charming Python" series of articles on IBM developerWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchType=1=dW=dW=charming+python HTH. -- _nimrod_a_abing_ http://abing.gotdns.com/ http://www.preownedcar.com/ --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: passing foreign key via post
> Why not create it there? That is the moment you have all the data you > need and all you're doing is calling a constructor. The code you've > written there is a pretty normal pattern for taking form information and > putting it into a model. Because it forces me to make different view for each model. Its very difficult for novice to understand which logic should be placed in views, which on models. Is there a resource with django-patterns or good programming practices ? > By the way, you might want to investigate form_for_model() (see the > newforms documentation again) if you are taking all your form data and > putting it into one model. Its pretty useful thing but I not for relation like Model -> Usermodel, which user object should be taken from request.user not from POST like in exampe above. > > Regards, > Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: passing foreign key via post
On Sat, 2007-06-09 at 14:38 +, Car wrote: > Thanks for your answer > > Now I have following snippet: > http://dpaste.com/11938/ > > But, is at a good practice to create Model objects in a view ? Where > code of that kind of operations should be placed? Why not create it there? That is the moment you have all the data you need and all you're doing is calling a constructor. The code you've written there is a pretty normal pattern for taking form information and putting it into a model. By the way, you might want to investigate form_for_model() (see the newforms documentation again) if you are taking all your form data and putting it into one model. That would mean you would create the form and then form.save() will push all the form fields into the model and save them. It's another possibility, not compulsory by any means (and I strongly encourage anybody who first learns to do forms manually, in any case -- you won't later be worried about having to go back to this method to do anything complex). Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: passing foreign key via post
Thanks for your answer Now I have following snippet: http://dpaste.com/11938/ But, is at a good practice to create Model objects in a view ? Where code of that kind of operations should be placed? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: passing foreign key via post
On Sat, 2007-06-09 at 13:39 +, Car wrote: > Hi, > > I have 1:n Relation, model A which hase foreign key ("type") to B. I'm > passing foreign key to a view via post using the ModelChoiceField. > Problem is its converted into string, so when I try to create in a > view a 'A' record > > like > # data = POST dictionary > > A( > address = data[ 'address' ], # okay becouse > address = charfield > gateuser = data[ 'gateuser' ], # still okay > type = ... > > And now I have type error - type should be a B-type not string. > > I can try smth like this: type B.objects.get(pk=int(data['type']) - > but is it a simplier and cleaner way to convert data passed via POST > from string to objects. You should never access POST data like this to work with it in your code, because (a) it will all be strings, as you've noticed and (b) you are bypassing all of the field validation code in the form, so your data might not be at all valid (what if the client submitted 'elephant' where you expected a number?). Have a read of the newforms documentation on the website (or in docs/newforms.txt in the source). Look at the is_valid() method. You will normally want to do something like this: form = MyForm(request.POST) if not form.is_valid(): # do something about the errors... else: # work with form.cleaned_data If you want specific field cleaning that isn't provided by the default field classes, have a read of http://groups.google.com/group/django-users/msg/37fe68ea76416872 (which will soon be in the documentation). Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---