This is the model relation:
class Tag(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=500, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
user = models.ForeignKey(User)
tagfield = models.ManyToManyField(Tag)
title = models.CharField(max_length=100)
content = models.TextField()
created = models.DateTimeField(default=datetime.datetime.now)
modified = models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
return '%s,%s' % (self.title,self.content)
class PostModelForm(forms.ModelForm):
class Meta:
model = Post
class PostModelFormNormalUser(forms.ModelForm):
class Meta:
model = Post
exclude = ('user', 'created', 'modified')
in views.py:
form = PostModelFormNormalUser()
context = {'form':form}
return render_to_response('addpost.html', context, context_instance=RequestContext(request))
in add.html: `{{ form.as_p }}
The title,content and select input is being displayed in webpage.
<p><label for="id_tagfield">Tagfield:</label> <select multiple="multiple" name="tagfield" id="id_tagfield">
</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
How can i get textbox instead of input for name = models.CharField(max_length=100).
Tag.name would be string seperated by spaces. I need to ‘title,content,tagname’ to display on webpage.
See https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
You’ll want something like this:
Then you’ll need to handle the POST data yourself in the view.