Re: Pre-populating forms with foreign keys
I do something similar in my app. I display a whole bunch of posts to the user, and they can choose to reply to any one. In my case all the displayed posts are in a single form. Associated with each post is a reply button whose onclick handler makes a call to a jquery function I wrote called addReply(). When the user clicks reply, it calls addReply (), passing in the id of the post as an argument. That function creates a hidden input called commentParent whose value is the id of the parent post. It also opens a text area where the user can type and adds cancel, preview (my comments are in a markup language), and save buttons. By creating the hidden commentParent input on the fly, I guarantee there is only one commentParent posted. My app has a requirement that the user can only reply to one comment at a time, so for this reason , my addReply() function disables all other reply buttons so that the useer can't reply to two different posts at once and when a reply is posted, the commentParent input is posted and my server picks that up along with the reply and adds the reply to the database. I am using Eric Florenzano's threadedcomments package for my comments, and that has worked well. Maybe there is standard stuff out there for doing this. I was a newbie at jquery when I started all of this and it was a major learning experience to get it all right. But as I type this, it really makes me wonder if there would have been something more canned that I could have used. What I did was pretty custom, but it certainly sounds like what a million other web apps out there do. Would be interested to hear what others are doing in this area. Margie On Dec 16, 9:31 am, Stewart wrote: > Disclaimer: This is my first Django adventure, please be gentle. > > I am currently working on a model that has a foreign key pointing to > itself. This foreign key is not mandatory. Think of a post in a forum. > The post will have a number of replies. The post itself will not have > a foreign key however each reply to the post will have a foreign key > of the initial post. So far so good, I have managed to set up the > model correctly. > > I am having a little trouble with the form. I am not sure of the best > way to lay it out for Django. I was initially thinking that I could > pre-populate an integer field with the widget type set to hidden. So > for example if I am viewing posts/34 and I click on reply the foreign > key field will be hidden and auto populated to 34. When the form gets > submitted the record gets created with the correct foreign key. > > Am my approaching this in the correct way? Is there a correct "Django" > way to do this that I have missed? > > Thanks in advance for any help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Pre-populating forms with foreign keys
Not sure on the Django side, but I know that most forum software uses a separate table for threads, having the posts refer back to the thread_id. The first post, would obviously be the topic of the thread. On Wed, Dec 16, 2009 at 9:31 AM, Stewart wrote: > Disclaimer: This is my first Django adventure, please be gentle. > > I am currently working on a model that has a foreign key pointing to > itself. This foreign key is not mandatory. Think of a post in a forum. > The post will have a number of replies. The post itself will not have > a foreign key however each reply to the post will have a foreign key > of the initial post. So far so good, I have managed to set up the > model correctly. > > I am having a little trouble with the form. I am not sure of the best > way to lay it out for Django. I was initially thinking that I could > pre-populate an integer field with the widget type set to hidden. So > for example if I am viewing posts/34 and I click on reply the foreign > key field will be hidden and auto populated to 34. When the form gets > submitted the record gets created with the correct foreign key. > > Am my approaching this in the correct way? Is there a correct "Django" > way to do this that I have missed? > > Thanks in advance for any help. > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Pre-populating forms with foreign keys
Disclaimer: This is my first Django adventure, please be gentle. I am currently working on a model that has a foreign key pointing to itself. This foreign key is not mandatory. Think of a post in a forum. The post will have a number of replies. The post itself will not have a foreign key however each reply to the post will have a foreign key of the initial post. So far so good, I have managed to set up the model correctly. I am having a little trouble with the form. I am not sure of the best way to lay it out for Django. I was initially thinking that I could pre-populate an integer field with the widget type set to hidden. So for example if I am viewing posts/34 and I click on reply the foreign key field will be hidden and auto populated to 34. When the form gets submitted the record gets created with the correct foreign key. Am my approaching this in the correct way? Is there a correct "Django" way to do this that I have missed? Thanks in advance for any help. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.