Django Forms, Foreign Key and Initial retuen all associated values

Posted by gramware on Stack Overflow See other posts from Stack Overflow or by gramware
Published on 2010-03-15T12:52:55Z Indexed on 2010/03/15 22:49 UTC
Read the original article Hit count: 653

Filed under:

I a working with Django forms. The issue I have is that Foreign Key fields and those using initial take all associated entries (all records associated with that record other then the one entry i wanted e.g instead of getting a primary key, i get the primary key, post subject, post body and all other values attributed with that record). The form and the other associated queries still work well, but this behaviour is clogging my database. How do i get the specific field i want instead of all records. An example of my models is here:

A form field for childParentId returns postID, postSubject and postBody instead of postID alone.

Also form = ForumCommentForm(initial = {'postSubject':forum.objects.get(postID = postID), }) returns all records related to postID.

class forum(models.Model):
postID = models.AutoField(primary_key=True)
postSubject = models.CharField(max_length=25)
postBody = models.TextField()
postPoster = models.ForeignKey(UserProfile)
postDate =  models.DateTimeField(auto_now_add=True)
child = models.BooleanField()
childParentId = models.ForeignKey('self',blank=True, null=True)
deleted = models.BooleanField()

def __unicode__(self):
    return u'%s %s %s %s %s' % (self.postSubject, self.postBody, self.postPoster, self.postDate, self.postID

© Stack Overflow or respective owner

Related posts about django-forms