2011-01-31 49 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__有關?如果將CreateD的def更改爲返回self.db,它是否工作? – PhoebeB 2011-01-31 15:35:45

回答