django - Diango 1.6 How about this way of create an object for a model which has two ForeignKey fields? -


there 3 models: user, question , answer.

my way can works--override answer modelform's save method, pass 2 objects(question , user it.

i not sure whether official way or best way. or there tutorial situation, thanks.

this code:

#settings.py  #root urls.py urlpatterns = patterns('',     url(r'^$', 'myauth.views.home', name='home'),     url(r'^questions/', include('questions.urls',namespace='questions')), )  #urls.py urlpatterns = patterns('questions.views',       url(r'^(?p<question_pk>\d+)/detail/$',         'detail',         {'template_name': 'questions/detail.html'},         name='detail'),                        )  #models.py class user(abstractbaseuser):     username = models.charfield()  class question(models.model):     user = models.foreignkey(user)       title = models.charfield()       content = models.textfield()  class answer(models.model):      #user create anwer question     user = models.foreignkey(user)       question = models.foreignkey(question)       content = models.textfield()   #forms.py class answerform(modelform):     class meta:         model = answer         fields = ['content']      #this way     #pass 2 objects:user , question     def save(self,user,question,commit=true,):         answer = super(answerform,self).save(commit=false)         answer.user = user         answer.question = question         if commit:             answer.save()         return answer  #views.py  def detail(request,     question_pk,     template_name,     answer_create_form=answerform):      """detail show question,contains:     1.question's title , content.     2.all answers(represented field 'content')to question     3.a form create answer question     """      #question object identified question_pk url string.     #along request.user, both passed form     #create answer objects.     question = question.objects.get(pk=question_pk)     user = request.user      #all answers question     answers = question.answer_set.all()     form=none     if user.is_authenticated():         if request.method == 'post':             form = answer_create_form(data=request.post)             if form.is_valid():                 form.save(user=user,question=question)                 return httpresponseredirect(request.meta.get('http_referer', '/'))         else:             form = answer_create_form()     return templateresponse(request, template_name, locals(),)  #questions/detail.html  #rest ommitted... {% if form %} <form action="" method="post">{% csrf_token %}         {{ form.as_p }}         <input type="submit" value="回答" /> </form> {% endif %} 

i think need read this. should de like:

question = question.objects.get(pk=question_pk) user = request.user (....) if user.is_authenticated():     if request.method == 'post':         answer = answer(question=question, user=user)         form = answerform(data=request.post, instance=answer)         if form.is_valid():             form.save()         (....) 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -