2016-07-07 9 views
1

すぐに外部キーに数値を追加するためのアプリケーションを構築します。モデル管理のDjangoでForeignKeyのすべてのインスタンスのすべての属性を編集します

私はDjangoの新機能を既に知っており、ここで関連する質問やDjangoのドキュメントを探しました。私が見つけたのはインラインAdminModelでしたが、それは本当に私が望むものではありません。

from django.db import models 
from django.utils.encoding import python_2_unicode_compatible 
import datetime 
from django.utils import timezone 


class Question(models.Model): 
    question_text = models.CharField(max_length=200) 
    pub_date= models.DateTimeField('date published') 

    def __str__(self): 
     return self.question_text 


class Choice(models.Model): 
    question = models.ForeignKey(Question, on_delete= models.CASCADE) 
    choice_text = models.CharField(max_length = 200) 
    votes = models.IntegerField(default=0) 
    def __str__(self): 
     return self.choice_text 

私はチュートリアルのコードを使用して、例えばForeignKey.Soのすべてのインスタンスを持つテーブルを持っていると思います、同時に、すべての質問にすべてのchoice_textと票を編集することが可能なはずである(1必ずしもそのようにコンテンツを追加するために異なる質問の間を飛び越えなければならない)。これは可能ですか? ご協力いただきありがとうございます!

Here a mock-up of the project:

答えて

0

あなたの問題は、あなたが間違った位置から始めているということです、そしてあなたはあなたが達成したいかを説明するためのトラブルを持っていた理由です。 Questionモデルから始めようとしているうちに、Choiceを一括(メインの問題)に更新し、関連する質問のテキストを表示します(セカンダリ問題)。

class ChoiceAdmin(admin.ModelAdmin): 
    list_display = ['__str__', 'question_text', 'choice_text', 'votes'] 
    list_editable = ['choice_text', 'votes'] 
    ordering = 'question' 

    def question_text(self, obj): 
     return obj.question.question_text 
    question_text.short_description = "Question" 

あなたはこのlist_editableを使用して一括でChoice Sを編集することができます

0

Django reference for the meta apiを見てください。
reference for model fieldsをご覧ください。
これらを組み合わせて問題を解決する必要があります。 擬似コード、

all_fields = MyModel._meta.fields 
for field in all_fields: 
    //Check if field is foreign key, field.get_internal_key() should work. 
    //Find foreign key 
関連する問題