Thanks for  your feedback. I will implement the changes that were suggested.



-------------- Original message -------------- 
From: Daniel Roseman <roseman.dan...@googlemail.com> 

> 
> On Jan 7, 7:47 am, jeffhg58 wrote: 
> > I have a 2 forms. One to add a new author and then another form for 
> > Articles which has a drop down list to reference the authors. The 
> > behavior I am seeing is that when I add a new author and then go to 
> > the New Article form which references the author it does not display 
> > the new record. Also, I put in some print statements in the New 
> > Article form class and those statements are only executed when I go to 
> > my home page. 
> > 
> > Here are my code snippets 
> > 
> > def welcome(request): 
> > 
> > print 'in welcome' 
> > 
> > if request.GET: 
> > if request.GET.has_key( 'newarticle'): 
> > 
> > print 'going to new article' 
> > 
> > return HttpResponseRedirect( '/melsite/newarticle') 
> > if request.GET.has_key( 'newauthor'): 
> > print 'going to new author' 
> > 
> > return HttpResponseRedirect( '/melsite/newauthor') 
> > else: 
> > 
> > print 'retrieving articles' 
> > 
> > articles = Article.objects.filter 
> > (type_index__type='Poem').order_by('title') 
> > 
> > return render_to_response('stories/welcome.html', {'title': 
> > 'Poems', 'articles': articles}) 
> > 
> > def newarticle( request): 
> > 
> > print 'in new article request' 
> > from storyforms import NewArticle 
> > 
> > if request.method == 'POST': 
> > print 'in new article post' 
> > new_data = request.POST.copy() 
> > articleform = NewArticle(new_data) 
> > if form.is_valid(): 
> > form.save( new_data) 
> > articles = Article.objects.filter 
> > (type_index__type='Poem').order_by('title') 
> > 
> > return render_to_response('stories/welcome.html', 
> > {'title': 'Poems', 'articles': articles}) 
> > else: 
> > print 'in setting up new article' 
> > #from storyforms import NewArticle 
> > articleform = NewArticle() 
> > print 'after setting up new article' 
> > print 'rendering article' 
> > return render_to_response('stories/add_story.html', {'form': 
> > articleform}) 
> > 
> > class NewArticle( forms.Form): 
> > 
> > articletypelist=[] 
> > authorlist = [] 
> > 
> > print 'in new article class' 
> > 
> > articletypes = ArticleType.objects.all().order_by( 'type') 
> > articletypelist.append( selecttuple) 
> > for articletype in articletypes: 
> > choicestr = "choice%s" % str( articletype.id) 
> > typetuple = choicestr, articletype.type 
> > articletypelist.append( typetuple) 
> > authors = Author.objects.all().order_by( 'last_name') 
> > print 'authors in new article are ', authors 
> > authorlist.append( selecttuple) 
> > for author in authors: 
> > choicestr = "choice%s" % str( author.id) 
> > authorname = author.first_name + ' ' + author.last_name 
> > authortuple = choicestr, authorname 
> > authorlist.append( authortuple) 
> > 
> > ArticleType = forms.ChoiceField( choices=articletypelist) 
> > Author = forms.ChoiceField( choices=authorlist) 
> > Title = forms.CharField() 
> > Content = forms.CharField( widget=forms.Textarea) 
> > print 'end of new article def' 
> > 
> > Thanks, 
> > Jeff 
> 
> As Karen says, logic in the class declaration is only executed once, 
> when the form is first imported. Authorlist and articletypelist are 
> much better implemented as class methods on the relevant models: 
> 
> class Author(models.Model): 
> ... 
> @classmethod 
> def get_authorlist(cls): 
> return [('choice%s' % a.id, '%s %s' % (a.first_name, 
> a.last_name)) 
> for a in cls.objects.all().order_by('last_name')] 
> 
> Then in your view or template you can do Author.get_authorlist() to 
> return the up-to-date list. 
> 
> However, a much better solution is to use ModelChoiceField for the 
> Articletype and Author fields. These populate the list dynamically 
> from a queryset: 
> 
> class NewArticle(forms.Form): 
> ... 
> Author = forms.ModelChoiceField(queryset=Author.objects.order_by 
> ('last_name')) 
> 
> -- 
> DR. 
> > 
--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to