How to introduce a text area as a field of a database entry

27 views
Skip to first unread message

joy

unread,
Dec 6, 2012, 6:18:38 AM12/6/12
to django...@googlegroups.com
My model.py

##model.py
from django.db import models

class Text(models.Model):
           pag= models.CharField(max_length=10)  
           content = models.CharField(max_length=500) 
##should i put something different in the previous line to have a textarea in the admin view of this field?

           def __unicode__(self):
                       return self.pag

thanks Joy

Nikhil Verma

unread,
Dec 6, 2012, 6:20:13 AM12/6/12
to django...@googlegroups.com
Try this

content = models.TextField(blank=True,null=True)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/z16sMIJfAT0J.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Regards
Nikhil Verma
+91-958-273-3156

Javier Guerra Giraldez

unread,
Dec 6, 2012, 8:58:47 AM12/6/12
to django...@googlegroups.com
On Thu, Dec 6, 2012 at 6:18 AM, joy <agnese.c...@gmail.com> wrote:
> content = models.CharField(max_length=500)

is that size even supported?

models.CharField maps to a VARCHAR() field, which AFAIK, is typically
limited to 255 chars. better use a models.TextField(), it maps to a
TEXT field, which is intended to longer runs of text; typically goes
to 64K.

and on the web form side, the default widget for a models.CharField()
is <input type="text"/>, while for a models.TextField it defaults to
<textarea>

--
Javier

Daniel Roseman

unread,
Dec 6, 2012, 4:39:40 PM12/6/12
to django...@googlegroups.com
On Thursday, 6 December 2012 13:58:47 UTC, Javier Guerra wrote:
On Thu, Dec 6, 2012 at 6:18 AM, joy <agnese.c...@gmail.com> wrote:
>  content = models.CharField(max_length=500)

is that size even supported?

models.CharField maps to a VARCHAR() field, which AFAIK, is typically
limited to 255 chars.

No, it isn't. MySQL has a limit of 65,536 bytes (which is actually shared among all columns in a row); PostgreSQL appears to have a 1GB limit per field. Certainly there can be good reasons for using a long CharField rather than a TextField: again in MySQL's InnoDB, a TEXT field is a blob, which means it is stored separately from the rest of the row, making retrieval more expensive, and causing JOINs to write temporary indexes to disk. Although admittedly the opposite is true in Postgres; it stores a VARCHAR as a text field.
--
DR.

Pedro J. Aramburu

unread,
Dec 6, 2012, 9:48:04 PM12/6/12
to django...@googlegroups.com
CharField is for "short" strings mostly under 255 of length. TextField is for "unlimited" text (the length depends of the database engine). If you just want a textarea for the CharField then you will need to replace the default widget. You'll need to read a little about Forms to change the widget of the field (https://docs.djangoproject.com/en/1.4/ref/forms/widgets/) and the Admin interface to see how to set a custom Form.
Reply all
Reply to author
Forward
0 new messages