2011-01-31 10 views
0

ForeignKeyにリンクされているフォームを作成しようとしています。私はあなたの質問を取得する場合DjangoのForeignKeyに関する問題

Model.py 

    enter code here 
    model.py 

class CreateD(models.Model): 
    id=models.IntegerField(primary_key=True) 
    db = models.CharField(max_length=50, blank=True) 
    iss = models.CharField(max_length=100, blank=True) 
    status = models.CharField(max_length=2, blank=True ,default='A') 
    class Meta: 
     db_table = u'add_d' 

    def __unicode__(self): 
     return u'%s %s' % (self.db,self.iss) 


class Assignment(models.Model): 
    id = models.IntegerField(primary_key=True) 
    assign_to = models.CharField(max_length=50, blank=True) 
    db_name = models.ForeignKey(CreateDb,related_name='set__dbname') 
    issue_type = models.ForeignKey(CreateDb,related_name='set_issuetype') 
    date = models.DateTimeField(null=True, blank=True) 
    class Meta: 
     db_table = u'assignment' 
    def __unicode__(self): 
     return u'%s %s' % (self.db_name,self.issue_type) 

form.py 

class AssignmentForm(ModelForm): 


class Meta: 
    model = Assignment 
    exclude = ('id','date','assign_to') 


assign.html 

    {{forms.as_p}} 


Data in the sql : 
    id db iss 
    1 A AB 
    2 B BA 




Output genrated from html page is 

    db_name : A AB (choice field) 
       B BA 
    issue_type : A AB (choice field) 
       B BA 


Actually i Need a output like below in html: 

    db_name : A (choice field) 
       B 
    issue_type : AB (choice field) 
       BA 

右のあなたは、彼らがを介して生成され、デフォルトでは、選択ボックスのラベルを変更したい...........この上

+0

私は理解していますが、あなたが定義した__unicode__とは関係がありますか?代わりにself.dbを返すようにCreateDのdefを変更すると、それは機能しますか? – PhoebeB

+0

何がうまくいかないかを説明すると助けになります。 –

+0

私はdb_nameの下にのみdbを取ってiss_type.butの下にissしたい。私の問題は選択肢のフィールドにある。私はdbとissの両方の選択フィールドを取得している。 – sush

答えて

1

を助けてくださいモデルの__unicode__メソッドですが、フォームフィールドのlabel_from_instanceメソッドをオーバーライドして、これを行う別の方法を選択することもできます。 Django forms: how to dynamically create ModelChoiceField labelsを参照してください。

関連する問題