| 32 | | Django will use ``first_name`` and ``last_name`` as the database column names. |
|---|
| 33 | | |
|---|
| 34 | | Each field type, except for ``ForeignKey``, ``ManyToManyField`` and |
|---|
| 35 | | ``OneToOneField``, takes an optional first positional argument -- a |
|---|
| 36 | | human-readable name. If the human-readable name isn't given, Django will |
|---|
| 37 | | automatically create the human-readable name by using the machine-readable |
|---|
| 38 | | name, converting underscores to spaces. |
|---|
| 39 | | |
|---|
| 40 | | In this example, the human-readable name is ``"Person's first name"``:: |
|---|
| 41 | | |
|---|
| 42 | | first_name = models.CharField("Person's first name", maxlength=30) |
|---|
| 43 | | |
|---|
| 44 | | In this example, the human-readable name is ``"first name"``:: |
|---|
| 45 | | |
|---|
| 46 | | first_name = models.CharField(maxlength=30) |
|---|
| 47 | | |
|---|
| 48 | | ``ForeignKey``, ``ManyToManyField`` and ``OneToOneField`` require the first |
|---|
| 49 | | argument to be a model class, so use the ``verbose_name`` keyword argument to |
|---|
| 50 | | specify the human-readable name:: |
|---|
| 51 | | |
|---|
| 52 | | poll = models.ForeignKey(Poll, verbose_name="the related poll") |
|---|
| 53 | | sites = models.ManyToManyField(Site, verbose_name="list of sites") |
|---|
| 54 | | place = models.OneToOneField(Place, verbose_name="related place") |
|---|
| 55 | | |
|---|
| 56 | | Convention is not to capitalize the first letter of the ``verbose_name``. |
|---|
| 57 | | Django will automatically capitalize the first letter where it needs to. |
|---|
| | 37 | ``first_name`` and ``last_name`` are *fields* of the model. Each field is |
|---|
| | 38 | specified as a class attribute, and each attribute maps to a database column. |
|---|
| | 39 | |
|---|
| | 40 | The above ``Person`` model would create an SQL table like this:: |
|---|
| | 41 | |
|---|
| | 42 | CREATE TABLE myapp_person ( |
|---|
| | 43 | "id" serial NOT NULL PRIMARY KEY, |
|---|
| | 44 | "first_name" varchar(30) NOT NULL, |
|---|
| | 45 | "last_name" varchar(30) NOT NULL |
|---|
| | 46 | ); |
|---|
| | 47 | |
|---|
| | 48 | Three technical notes: |
|---|
| | 49 | |
|---|
| | 50 | * The name of the table, ``myapp_person``, is automatically derived from |
|---|
| | 51 | some model metadata but can be overridden. See _`Table names` below. |
|---|
| | 52 | * An ``id`` field is added automatically, but this behavior can be |
|---|
| | 53 | overriden. See _`Automatic primary key fields` below. |
|---|
| | 54 | * The ``CREATE TABLE`` SQL in this example is formatted using PostgreSQL |
|---|
| | 55 | syntax, but it's worth noting Django uses SQL tailored to the database |
|---|
| | 56 | backend specified in your `settings file`_. |
|---|
| | 57 | |
|---|
| | 58 | .. _settings file: http://www.djangoproject.com/documentation/settings/ |
|---|
| | 59 | |
|---|
| | 60 | Fields |
|---|
| | 61 | ====== |
|---|
| | 62 | |
|---|
| | 63 | The most important part of a model -- and the only required part of a model -- |
|---|
| | 64 | is the list of database fields it defines. Fields are specified by class |
|---|
| | 65 | attributes. |
|---|
| | 66 | |
|---|
| | 67 | Example:: |
|---|
| | 68 | |
|---|
| | 69 | class Musician(models.Model): |
|---|
| | 70 | first_name = models.CharField(maxlength=50) |
|---|
| | 71 | last_name = models.CharField(maxlength=50) |
|---|
| | 72 | instrument = models.CharField(maxlength=100) |
|---|
| | 73 | |
|---|
| | 74 | class Album(models.Model): |
|---|
| | 75 | artist = models.ForeignKey(Musician) |
|---|
| | 76 | name = models.CharField(maxlength=100) |
|---|
| | 77 | release_date = models.DateField() |
|---|
| | 78 | num_stars = models.IntegerField() |
|---|
| 62 | | TODO: Fill this section. |
|---|
| 63 | | |
|---|
| 64 | | * Can't be Python reserved word. |
|---|
| 65 | | * Can't have more than one underscore in a row, due to query lookup. |
|---|
| 66 | | |
|---|
| 67 | | General field options |
|---|
| 68 | | --------------------- |
|---|
| | 83 | Django places only two restrictions on model field names: |
|---|
| | 84 | |
|---|
| | 85 | 1. A field name cannot be a Python reserved word, because that would result |
|---|
| | 86 | in a Python syntax error. For example:: |
|---|
| | 87 | |
|---|
| | 88 | class Example(models.Model): |
|---|
| | 89 | pass = models.IntegerField() # 'pass' is a reserved word! |
|---|
| | 90 | |
|---|
| | 91 | 2. A field name cannot contain more than one underscore in a row, due to |
|---|
| | 92 | the way Django's query lookup syntax works. For example:: |
|---|
| | 93 | |
|---|
| | 94 | class Example(models.Model): |
|---|
| | 95 | foo__bar = models.IntegerField() 'foo__bar' has two underscores! |
|---|
| | 96 | |
|---|
| | 97 | These limitations can be worked around, though, because your field name doesn't |
|---|
| | 98 | necessarily have to match your database column name. See `db_column`_ below. |
|---|
| | 99 | |
|---|
| | 100 | SQL reserved words, such as ``join``, ``where`` or ``select`, *are* allowed as |
|---|
| | 101 | model field names, because Django escapes all database table names and column |
|---|
| | 102 | names in every underlying SQL query. It uses the quoting syntax of your |
|---|
| | 103 | particular database engine. |
|---|
| | 104 | |
|---|
| | 105 | Field types |
|---|
| | 106 | ----------- |
|---|
| | 107 | |
|---|
| | 108 | Each field in your model should be an instance of the appropriate ``Field`` |
|---|
| | 109 | class. Django uses the field class types to determine a few things: |
|---|
| | 110 | |
|---|
| | 111 | * The database column type (e.g. ``INTEGER``, ``VARCHAR``). |
|---|
| | 112 | * The widget to use in Django's admin interface, if you care to use it |
|---|
| | 113 | (e.g. ``<input type="text">``, ``<select>``). |
|---|
| | 114 | * The minimal validation requirements, used in Django's admin and in |
|---|
| | 115 | manipulators. |
|---|
| | 116 | |
|---|
| | 117 | Here are all available field types: |
|---|
| | 118 | |
|---|
| | 119 | ``AutoField`` |
|---|
| | 120 | ~~~~~~~~~~~~~ |
|---|
| | 121 | |
|---|
| | 122 | An ``IntegerField`` that automatically increments according to available IDs. |
|---|
| | 123 | You usually won't need to use this directly; a primary key field will |
|---|
| | 124 | automatically be added to your model if you don't specify otherwise. See |
|---|
| | 125 | _`Automatic primary key fields`. |
|---|
| | 126 | |
|---|
| | 127 | ``BooleanField`` |
|---|
| | 128 | ~~~~~~~~~~~~~~~~ |
|---|
| | 129 | |
|---|
| | 130 | A true/false field. |
|---|
| | 131 | |
|---|
| | 132 | The admin represents this as a checkbox. |
|---|
| | 133 | |
|---|
| | 134 | ``CharField`` |
|---|
| | 135 | ~~~~~~~~~~~~~ |
|---|
| | 136 | |
|---|
| | 137 | A string field, for small- to large-sized strings. |
|---|
| | 138 | |
|---|
| | 139 | For large amounts of text, use ``TextField``. |
|---|
| | 140 | |
|---|
| | 141 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 142 | |
|---|
| | 143 | ``CharField`` has an extra required argument, ``maxlength``, the maximum length |
|---|
| | 144 | (in characters) of the field. The maxlength is enforced at the database level |
|---|
| | 145 | and in Django's validation. |
|---|
| | 146 | |
|---|
| | 147 | ``CommaSeparatedIntegerField`` |
|---|
| | 148 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 149 | |
|---|
| | 150 | A field of integers separated by commas. As in ``CharField``, the ``maxlength`` |
|---|
| | 151 | argument is required. |
|---|
| | 152 | |
|---|
| | 153 | ``DateField`` |
|---|
| | 154 | ~~~~~~~~~~~~~ |
|---|
| | 155 | |
|---|
| | 156 | A date field. Has a few extra optional arguments: |
|---|
| | 157 | |
|---|
| | 158 | ====================== =================================================== |
|---|
| | 159 | Argument Description |
|---|
| | 160 | ====================== =================================================== |
|---|
| | 161 | ``auto_now`` Automatically set the field to now every time the |
|---|
| | 162 | object is saved. Useful for "last-modified" |
|---|
| | 163 | timestamps. |
|---|
| | 164 | |
|---|
| | 165 | ``auto_now_add`` Automatically set the field to now when the object |
|---|
| | 166 | is first created. Useful for creation of |
|---|
| | 167 | timestamps. |
|---|
| | 168 | ====================== =================================================== |
|---|
| | 169 | |
|---|
| | 170 | The admin represents this as an ``<input type="text">`` with a JavaScript |
|---|
| | 171 | calendar and a shortcut for "Today." |
|---|
| | 172 | |
|---|
| | 173 | ``DateTimeField`` |
|---|
| | 174 | ~~~~~~~~~~~~~~~~~ |
|---|
| | 175 | |
|---|
| | 176 | A date and time field. Takes the same extra options as ``DateField``. |
|---|
| | 177 | |
|---|
| | 178 | The admin represents this as two ``<input type="text">`` fields, with |
|---|
| | 179 | JavaScript shortcuts. |
|---|
| | 180 | |
|---|
| | 181 | ``EmailField`` |
|---|
| | 182 | ~~~~~~~~~~~~~~ |
|---|
| | 183 | |
|---|
| | 184 | A ``CharField`` that checks that the value is a valid e-mail address. |
|---|
| | 185 | This doesn't accept ``maxlength``. |
|---|
| | 186 | |
|---|
| | 187 | ``FileField`` |
|---|
| | 188 | ~~~~~~~~~~~~~ |
|---|
| | 189 | |
|---|
| | 190 | A file-upload field. |
|---|
| | 191 | |
|---|
| | 192 | Has an extra required argument, ``upload_to``, a local filesystem path to |
|---|
| | 193 | which files should be upload. This path may contain `strftime formatting`_, |
|---|
| | 194 | which will be replaced by the date/time of the file upload (so that |
|---|
| | 195 | uploaded files don't fill up the given directory). |
|---|
| | 196 | |
|---|
| | 197 | The admin represents this as an ``<input type="file">`` (a file-upload widget). |
|---|
| | 198 | |
|---|
| | 199 | Using a ``FileField`` or an ``ImageField`` (see below) in a model takes a few |
|---|
| | 200 | steps: |
|---|
| | 201 | |
|---|
| | 202 | 1. In your settings file, you'll need to define ``MEDIA_ROOT`` as the |
|---|
| | 203 | full path to a directory where you'd like Django to store uploaded |
|---|
| | 204 | files. (For performance, these files are not stored in the database.) |
|---|
| | 205 | Define ``MEDIA_URL`` as the base public URL of that directory. Make |
|---|
| | 206 | sure that this directory is writable by the Web server's user |
|---|
| | 207 | account. |
|---|
| | 208 | |
|---|
| | 209 | 2. Add the ``FileField`` or ``ImageField`` to your model, making sure |
|---|
| | 210 | to define the ``upload_to`` option to tell Django to which |
|---|
| | 211 | subdirectory of ``MEDIA_ROOT`` it should upload files. |
|---|
| | 212 | |
|---|
| | 213 | 3. All that will be stored in your database is a path to the file |
|---|
| | 214 | (relative to ``MEDIA_ROOT``). You'll must likely want to use the |
|---|
| | 215 | convenience ``get_<fieldname>_url`` function provided by Django. For |
|---|
| | 216 | example, if your ``ImageField`` is called ``mug_shot``, you can get |
|---|
| | 217 | the absolute URL to your image in a template with ``{{ |
|---|
| | 218 | object.get_mug_shot_url }}``. |
|---|
| | 219 | |
|---|
| | 220 | .. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941 |
|---|
| | 221 | |
|---|
| | 222 | ``FilePathField`` |
|---|
| | 223 | ~~~~~~~~~~~~~~~~~ |
|---|
| | 224 | |
|---|
| | 225 | A field whose choices are limited to the filenames in a certain directory |
|---|
| | 226 | on the filesystem. Has three special arguments, of which the first is |
|---|
| | 227 | required: |
|---|
| | 228 | |
|---|
| | 229 | ====================== =================================================== |
|---|
| | 230 | Argument Description |
|---|
| | 231 | ====================== =================================================== |
|---|
| | 232 | ``path`` Required. The absolute filesystem path to a |
|---|
| | 233 | directory from which this ``FilePathField`` should |
|---|
| | 234 | get its choices. Example: ``"/home/images"``. |
|---|
| | 235 | |
|---|
| | 236 | ``match`` Optional. A regular expression, as a string, that |
|---|
| | 237 | ``FilePathField`` will use to filter filenames. |
|---|
| | 238 | Note that the regex will be applied to the |
|---|
| | 239 | base filename, not the full path. Example: |
|---|
| | 240 | ``"foo.*\.txt^"``, which will match a file called |
|---|
| | 241 | ``foo23.txt`` but not ``bar.txt`` or ``foo23.gif``. |
|---|
| | 242 | |
|---|
| | 243 | ``recursive`` Optional. Either ``True`` or ``False``. Default is |
|---|
| | 244 | ``False``. Specifies whether all subdirectories of |
|---|
| | 245 | ``path`` should be included. |
|---|
| | 246 | ====================== =================================================== |
|---|
| | 247 | |
|---|
| | 248 | Of course, these arguments can be used together. |
|---|
| | 249 | |
|---|
| | 250 | The one potential gotcha is that ``match`` applies to the base filename, |
|---|
| | 251 | not the full path. So, this example:: |
|---|
| | 252 | |
|---|
| | 253 | FilePathField(path="/home/images", match="foo.*", recursive=True) |
|---|
| | 254 | |
|---|
| | 255 | ...will match ``/home/images/foo.gif`` but not ``/home/images/foo/bar.gif`` |
|---|
| | 256 | because the ``match`` applies to the base filename (``foo.gif`` and |
|---|
| | 257 | ``bar.gif``). |
|---|
| | 258 | |
|---|
| | 259 | ``FloatField`` |
|---|
| | 260 | ~~~~~~~~~~~~~~ |
|---|
| | 261 | |
|---|
| | 262 | A floating-point number. Has two **required** arguments: |
|---|
| | 263 | |
|---|
| | 264 | ====================== =================================================== |
|---|
| | 265 | Argument Description |
|---|
| | 266 | ====================== =================================================== |
|---|
| | 267 | ``max_digits`` The maximum number of digits allowed in the number. |
|---|
| | 268 | |
|---|
| | 269 | ``decimal_places`` The number of decimal places to store with the |
|---|
| | 270 | number. |
|---|
| | 271 | ====================== =================================================== |
|---|
| | 272 | |
|---|
| | 273 | For example, to store numbers up to 999 with a resolution of 2 decimal places, |
|---|
| | 274 | you'd use:: |
|---|
| | 275 | |
|---|
| | 276 | models.FloatField(..., max_digits=5, decimal_places=2) |
|---|
| | 277 | |
|---|
| | 278 | And to store numbers up to approximately one billion with a resolution of 10 |
|---|
| | 279 | decimal places:: |
|---|
| | 280 | |
|---|
| | 281 | models.FloatField(..., max_digits=19, decimal_places=10) |
|---|
| | 282 | |
|---|
| | 283 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 284 | |
|---|
| | 285 | ``ImageField`` |
|---|
| | 286 | ~~~~~~~~~~~~~~ |
|---|
| | 287 | |
|---|
| | 288 | Like ``FileField``, but validates that the uploaded object is a valid |
|---|
| | 289 | image. Has two extra optional arguments, ``height_field`` and |
|---|
| | 290 | ``width_field``, which, if set, will be auto-populated with the height and |
|---|
| | 291 | width of the image each time a model instance is saved. |
|---|
| | 292 | |
|---|
| | 293 | Requires the `Python Imaging Library`_. |
|---|
| | 294 | |
|---|
| | 295 | .. _Python Imaging Library: http://www.pythonware.com/products/pil/ |
|---|
| | 296 | |
|---|
| | 297 | ``IntegerField`` |
|---|
| | 298 | ~~~~~~~~~~~~~~~~ |
|---|
| | 299 | |
|---|
| | 300 | An integer. |
|---|
| | 301 | |
|---|
| | 302 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 303 | |
|---|
| | 304 | ``IPAddressField`` |
|---|
| | 305 | ~~~~~~~~~~~~~~~~~~ |
|---|
| | 306 | |
|---|
| | 307 | An IP address, in string format (i.e. "24.124.1.30"). |
|---|
| | 308 | |
|---|
| | 309 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 310 | |
|---|
| | 311 | ``NullBooleanField`` |
|---|
| | 312 | ~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 313 | |
|---|
| | 314 | Like a ``BooleanField``, but allows ``NULL`` as one of the options. Use this |
|---|
| | 315 | instead of a ``BooleanField`` with ``null=True``. |
|---|
| | 316 | |
|---|
| | 317 | The admin represents this as a ``<select>`` box with "Unknown", "Yes" and "No" choices. |
|---|
| | 318 | |
|---|
| | 319 | ``PhoneNumberField`` |
|---|
| | 320 | ~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 321 | |
|---|
| | 322 | A ``CharField`` that checks that the value is a valid U.S.A.-style phone |
|---|
| | 323 | number (in the format ``XXX-XXX-XXXX``). |
|---|
| | 324 | |
|---|
| | 325 | ``PositiveIntegerField`` |
|---|
| | 326 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 327 | |
|---|
| | 328 | Like an ``IntegerField``, but must be positive. |
|---|
| | 329 | |
|---|
| | 330 | ``PositiveSmallIntegerField`` |
|---|
| | 331 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 332 | |
|---|
| | 333 | Like a ``PositiveIntegerField``, but only allows values under a certain |
|---|
| | 334 | (database-dependent) point. |
|---|
| | 335 | |
|---|
| | 336 | ``SlugField`` |
|---|
| | 337 | ~~~~~~~~~~~~~ |
|---|
| | 338 | |
|---|
| | 339 | "Slug" is a newspaper term. A slug is a short label for something, |
|---|
| | 340 | containing only letters, numbers, underscores or hyphens. They're generally |
|---|
| | 341 | used in URLs. |
|---|
| | 342 | |
|---|
| | 343 | In the Django development version, you can specify ``maxlength``. If |
|---|
| | 344 | ``maxlength`` is not specified, Django will use a default length of 50. In |
|---|
| | 345 | previous Django versions, there's no way to override the length of 50. |
|---|
| | 346 | |
|---|
| | 347 | Implies ``db_index=True``. |
|---|
| | 348 | |
|---|
| | 349 | Accepts an extra option, ``prepopulate_from``, which is a list of fields |
|---|
| | 350 | from which to auto-populate the slug, via JavaScript, in the object's admin |
|---|
| | 351 | form:: |
|---|
| | 352 | |
|---|
| | 353 | models.SlugField(prepopulate_from=("pre_name", "name")) |
|---|
| | 354 | |
|---|
| | 355 | ``prepopulate_from`` doesn't accept DateTimeFields. |
|---|
| | 356 | |
|---|
| | 357 | The admin represents ``SlugField`` as an ``<input type="text">`` (a |
|---|
| | 358 | single-line input). |
|---|
| | 359 | |
|---|
| | 360 | ``SmallIntegerField`` |
|---|
| | 361 | ~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 362 | |
|---|
| | 363 | Like an ``IntegerField``, but only allows values under a certain |
|---|
| | 364 | (database-dependent) point. |
|---|
| | 365 | |
|---|
| | 366 | ``TextField`` |
|---|
| | 367 | ~~~~~~~~~~~~~ |
|---|
| | 368 | |
|---|
| | 369 | A large text field. |
|---|
| | 370 | |
|---|
| | 371 | The admin represents this as a ``<textarea>`` (a multi-line input). |
|---|
| | 372 | |
|---|
| | 373 | ``TimeField`` |
|---|
| | 374 | ~~~~~~~~~~~~~ |
|---|
| | 375 | |
|---|
| | 376 | A time. Accepts the same auto-population options as ``DateField`` and |
|---|
| | 377 | ``DateTimeField``. |
|---|
| | 378 | |
|---|
| | 379 | The admin represents this as an ``<input type="text">`` with some |
|---|
| | 380 | JavaScript shortcuts. |
|---|
| | 381 | |
|---|
| | 382 | ``URLField`` |
|---|
| | 383 | ~~~~~~~~~~~~ |
|---|
| | 384 | |
|---|
| | 385 | A field for a URL. If the ``verify_exists`` option is ``True`` (default), |
|---|
| | 386 | the URL given will be checked for existence (i.e., the URL actually loads |
|---|
| | 387 | and doesn't give a 404 response). |
|---|
| | 388 | |
|---|
| | 389 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 390 | |
|---|
| | 391 | ``USStateField`` |
|---|
| | 392 | ~~~~~~~~~~~~~~~~ |
|---|
| | 393 | |
|---|
| | 394 | A two-letter U.S. state abbreviation. |
|---|
| | 395 | |
|---|
| | 396 | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| | 397 | |
|---|
| | 398 | ``XMLField`` |
|---|
| | 399 | ~~~~~~~~~~~~ |
|---|
| | 400 | |
|---|
| | 401 | A ``TextField`` that checks that the value is valid XML that matches a |
|---|
| | 402 | given schema. Takes one required argument, ``schema_path``, which is the |
|---|
| | 403 | filesystem path to a RelaxNG_ schema against which to validate the field. |
|---|
| | 404 | |
|---|
| | 405 | .. _RelaxNG: http://www.relaxng.org/ |
|---|
| | 406 | |
|---|
| | 407 | Field options |
|---|
| | 408 | ------------- |
|---|
| 219 | | Field types |
|---|
| 220 | | ----------- |
|---|
| 221 | | |
|---|
| 222 | | Each field in your model should be an instance of the appropriate ``Field`` |
|---|
| 223 | | class. Django uses the field class types to determine a few things: |
|---|
| 224 | | |
|---|
| 225 | | * The database column type (e.g. ``INTEGER``, ``VARCHAR``). |
|---|
| 226 | | * The widget to use in Django's admin (e.g. ``<input type="text">``, ``<select>``). |
|---|
| 227 | | * The minimal validation requirements, used in Django's admin and in manipulators. |
|---|
| 228 | | |
|---|
| 229 | | Here are all available field types: |
|---|
| 230 | | |
|---|
| 231 | | ``AutoField`` |
|---|
| 232 | | An ``IntegerField`` that automatically increments according to available |
|---|
| 233 | | IDs. You usually won't need to use this directly; a primary key field will |
|---|
| 234 | | automatically be added to your model if you don't specify otherwise. (See |
|---|
| 235 | | ``primary_key`` in ``General field options`` above.) |
|---|
| 236 | | |
|---|
| 237 | | ``BooleanField`` |
|---|
| 238 | | A true/false field. |
|---|
| 239 | | |
|---|
| 240 | | The admin represents this as a checkbox. |
|---|
| 241 | | |
|---|
| 242 | | ``CharField`` |
|---|
| 243 | | A string field, for small- to large-sized strings. |
|---|
| 244 | | |
|---|
| 245 | | For large amounts of text, use ``TextField``. |
|---|
| 246 | | |
|---|
| 247 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 248 | | |
|---|
| 249 | | ``CharField`` has an extra required argument, ``maxlength``, the maximum |
|---|
| 250 | | length (in characters) of the field. The maxlength is enforced at the |
|---|
| 251 | | database level and in Django's validation. |
|---|
| 252 | | |
|---|
| 253 | | ``CommaSeparatedIntegerField`` |
|---|
| 254 | | A field of integers separated by commas. As in ``CharField``, the |
|---|
| 255 | | ``maxlength`` argument is required. |
|---|
| 256 | | |
|---|
| 257 | | ``DateField`` |
|---|
| 258 | | A date field. Has a few extra optional arguments: |
|---|
| 259 | | |
|---|
| 260 | | ====================== =================================================== |
|---|
| 261 | | Argument Description |
|---|
| 262 | | ====================== =================================================== |
|---|
| 263 | | ``auto_now`` Automatically set the field to now every time the |
|---|
| 264 | | object is saved. Useful for "last-modified" |
|---|
| 265 | | timestamps. |
|---|
| 266 | | |
|---|
| 267 | | ``auto_now_add`` Automatically set the field to now when the object |
|---|
| 268 | | is first created. Useful for creation of |
|---|
| 269 | | timestamps. |
|---|
| 270 | | ====================== =================================================== |
|---|
| 271 | | |
|---|
| 272 | | The admin represents this as an ``<input type="text">`` with a JavaScript |
|---|
| 273 | | calendar and a shortcut for "Today." |
|---|
| 274 | | |
|---|
| 275 | | ``DateTimeField`` |
|---|
| 276 | | A date and time field. Takes the same extra options as ``DateField``. |
|---|
| 277 | | |
|---|
| 278 | | The admin represents this as two ``<input type="text">`` fields, with |
|---|
| 279 | | JavaScript shortcuts. |
|---|
| 280 | | |
|---|
| 281 | | ``EmailField`` |
|---|
| 282 | | A ``CharField`` that checks that the value is a valid e-mail address. |
|---|
| 283 | | This doesn't accept ``maxlength``. |
|---|
| 284 | | |
|---|
| 285 | | ``FileField`` |
|---|
| 286 | | A file-upload field. |
|---|
| 287 | | |
|---|
| 288 | | Has an extra required argument, ``upload_to``, a local filesystem path to |
|---|
| 289 | | which files should be upload. This path may contain `strftime formatting`_, |
|---|
| 290 | | which will be replaced by the date/time of the file upload (so that |
|---|
| 291 | | uploaded files don't fill up the given directory). |
|---|
| 292 | | |
|---|
| 293 | | The admin represents this as an ``<input type="file">`` (a file-upload widget). |
|---|
| 294 | | |
|---|
| 295 | | Using a ``FileField`` or an ``ImageField`` (see below) in a model takes a few |
|---|
| 296 | | steps: |
|---|
| 297 | | |
|---|
| 298 | | 1. In your settings file, you'll need to define ``MEDIA_ROOT`` as the |
|---|
| 299 | | full path to a directory where you'd like Django to store uploaded |
|---|
| 300 | | files. (For performance, these files are not stored in the database.) |
|---|
| 301 | | Define ``MEDIA_URL`` as the base public URL of that directory. Make |
|---|
| 302 | | sure that this directory is writable by the Web server's user |
|---|
| 303 | | account. |
|---|
| 304 | | |
|---|
| 305 | | 2. Add the ``FileField`` or ``ImageField`` to your model, making sure |
|---|
| 306 | | to define the ``upload_to`` option to tell Django to which |
|---|
| 307 | | subdirectory of ``MEDIA_ROOT`` it should upload files. |
|---|
| 308 | | |
|---|
| 309 | | 3. All that will be stored in your database is a path to the file |
|---|
| 310 | | (relative to ``MEDIA_ROOT``). You'll must likely want to use the |
|---|
| 311 | | convenience ``get_<fieldname>_url`` function provided by Django. For |
|---|
| 312 | | example, if your ``ImageField`` is called ``mug_shot``, you can get |
|---|
| 313 | | the absolute URL to your image in a template with ``{{ |
|---|
| 314 | | object.get_mug_shot_url }}``. |
|---|
| 315 | | |
|---|
| 316 | | .. _`strftime formatting`: http://docs.python.org/lib/module-time.html#l2h-1941 |
|---|
| 317 | | |
|---|
| 318 | | ``FilePathField`` |
|---|
| 319 | | A field whose choices are limited to the filenames in a certain directory |
|---|
| 320 | | on the filesystem. Has three special arguments, of which the first is |
|---|
| 321 | | required: |
|---|
| 322 | | |
|---|
| 323 | | ====================== =================================================== |
|---|
| 324 | | Argument Description |
|---|
| 325 | | ====================== =================================================== |
|---|
| 326 | | ``path`` Required. The absolute filesystem path to a |
|---|
| 327 | | directory from which this ``FilePathField`` should |
|---|
| 328 | | get its choices. Example: ``"/home/images"``. |
|---|
| 329 | | |
|---|
| 330 | | ``match`` Optional. A regular expression, as a string, that |
|---|
| 331 | | ``FilePathField`` will use to filter filenames. |
|---|
| 332 | | Note that the regex will be applied to the |
|---|
| 333 | | base filename, not the full path. Example: |
|---|
| 334 | | ``"foo.*\.txt^"``, which will match a file called |
|---|
| 335 | | ``foo23.txt`` but not ``bar.txt`` or ``foo23.gif``. |
|---|
| 336 | | |
|---|
| 337 | | ``recursive`` Optional. Either ``True`` or ``False``. Default is |
|---|
| 338 | | ``False``. Specifies whether all subdirectories of |
|---|
| 339 | | ``path`` should be included. |
|---|
| 340 | | ====================== =================================================== |
|---|
| 341 | | |
|---|
| 342 | | Of course, these arguments can be used together. |
|---|
| 343 | | |
|---|
| 344 | | The one potential gotcha is that ``match`` applies to the base filename, |
|---|
| 345 | | not the full path. So, this example:: |
|---|
| 346 | | |
|---|
| 347 | | FilePathField(path="/home/images", match="foo.*", recursive=True) |
|---|
| 348 | | |
|---|
| 349 | | ...will match ``/home/images/foo.gif`` but not ``/home/images/foo/bar.gif`` |
|---|
| 350 | | because the ``match`` applies to the base filename (``foo.gif`` and |
|---|
| 351 | | ``bar.gif``). |
|---|
| 352 | | |
|---|
| 353 | | ``FloatField`` |
|---|
| 354 | | A floating-point number. Has two **required** arguments: |
|---|
| 355 | | |
|---|
| 356 | | ====================== =================================================== |
|---|
| 357 | | Argument Description |
|---|
| 358 | | ====================== =================================================== |
|---|
| 359 | | ``max_digits`` The maximum number of digits allowed in the number. |
|---|
| 360 | | |
|---|
| 361 | | ``decimal_places`` The number of decimal places to store with the |
|---|
| 362 | | number. |
|---|
| 363 | | ====================== =================================================== |
|---|
| 364 | | |
|---|
| 365 | | For example, to store numbers up to 999 with a resolution of 2 decimal places, |
|---|
| 366 | | you'd use:: |
|---|
| 367 | | |
|---|
| 368 | | models.FloatField(..., max_digits=5, decimal_places=2) |
|---|
| 369 | | |
|---|
| 370 | | And to store numbers up to approximately one billion with a resolution of 10 |
|---|
| 371 | | decimal places:: |
|---|
| 372 | | |
|---|
| 373 | | models.FloatField(..., max_digits=19, decimal_places=10) |
|---|
| 374 | | |
|---|
| 375 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 376 | | |
|---|
| 377 | | ``ImageField`` |
|---|
| 378 | | Like ``FileField``, but validates that the uploaded object is a valid |
|---|
| 379 | | image. Has two extra optional arguments, ``height_field`` and |
|---|
| 380 | | ``width_field``, which, if set, will be auto-populated with the height and |
|---|
| 381 | | width of the image each time a model instance is saved. |
|---|
| 382 | | |
|---|
| 383 | | Requires the `Python Imaging Library`_. |
|---|
| 384 | | |
|---|
| 385 | | .. _Python Imaging Library: http://www.pythonware.com/products/pil/ |
|---|
| 386 | | |
|---|
| 387 | | ``IntegerField`` |
|---|
| 388 | | An integer. |
|---|
| 389 | | |
|---|
| 390 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 391 | | |
|---|
| 392 | | ``IPAddressField`` |
|---|
| 393 | | An IP address, in string format (i.e. "24.124.1.30"). |
|---|
| 394 | | |
|---|
| 395 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 396 | | |
|---|
| 397 | | ``NullBooleanField`` |
|---|
| 398 | | Like a ``BooleanField``, but allows ``NULL`` as one of the options. Use this |
|---|
| 399 | | instead of a ``BooleanField`` with ``null=True``. |
|---|
| 400 | | |
|---|
| 401 | | The admin represents this as a ``<select>`` box with "Unknown", "Yes" and "No" choices. |
|---|
| 402 | | |
|---|
| 403 | | ``PhoneNumberField`` |
|---|
| 404 | | A ``CharField`` that checks that the value is a valid U.S.A.-style phone |
|---|
| 405 | | number (in the format ``XXX-XXX-XXXX``). |
|---|
| 406 | | |
|---|
| 407 | | ``PositiveIntegerField`` |
|---|
| 408 | | Like an ``IntegerField``, but must be positive. |
|---|
| 409 | | |
|---|
| 410 | | ``PositiveSmallIntegerField`` |
|---|
| 411 | | Like a ``PositiveIntegerField``, but only allows values under a certain |
|---|
| 412 | | (database-dependent) point. |
|---|
| 413 | | |
|---|
| 414 | | ``SlugField`` |
|---|
| 415 | | "Slug" is a newspaper term. A slug is a short label for something, |
|---|
| 416 | | containing only letters, numbers, underscores or hyphens. They're generally |
|---|
| 417 | | used in URLs. |
|---|
| 418 | | |
|---|
| 419 | | In the Django development version, you can specify ``maxlength``. If |
|---|
| 420 | | ``maxlength`` is not specified, Django will use a default length of 50. In |
|---|
| 421 | | previous Django versions, there's no way to override the length of 50. |
|---|
| 422 | | |
|---|
| 423 | | Implies ``db_index=True``. |
|---|
| 424 | | |
|---|
| 425 | | Accepts an extra option, ``prepopulate_from``, which is a list of fields |
|---|
| 426 | | from which to auto-populate the slug, via JavaScript, in the object's admin |
|---|
| 427 | | form:: |
|---|
| 428 | | |
|---|
| 429 | | models.SlugField(prepopulate_from=("pre_name", "name")) |
|---|
| 430 | | |
|---|
| 431 | | ``prepopulate_from`` doesn't accept DateTimeFields. |
|---|
| 432 | | |
|---|
| 433 | | The admin represents ``SlugField`` as an ``<input type="text">`` (a |
|---|
| 434 | | single-line input). |
|---|
| 435 | | |
|---|
| 436 | | ``SmallIntegerField`` |
|---|
| 437 | | Like an ``IntegerField``, but only allows values under a certain |
|---|
| 438 | | (database-dependent) point. |
|---|
| 439 | | |
|---|
| 440 | | ``TextField`` |
|---|
| 441 | | A large text field. |
|---|
| 442 | | |
|---|
| 443 | | The admin represents this as a ``<textarea>`` (a multi-line input). |
|---|
| 444 | | |
|---|
| 445 | | ``TimeField`` |
|---|
| 446 | | A time. Accepts the same auto-population options as ``DateField`` and |
|---|
| 447 | | ``DateTimeField``. |
|---|
| 448 | | |
|---|
| 449 | | The admin represents this as an ``<input type="text">`` with some |
|---|
| 450 | | JavaScript shortcuts. |
|---|
| 451 | | |
|---|
| 452 | | ``URLField`` |
|---|
| 453 | | A field for a URL. If the ``verify_exists`` option is ``True`` (default), |
|---|
| 454 | | the URL given will be checked for existence (i.e., the URL actually loads |
|---|
| 455 | | and doesn't give a 404 response). |
|---|
| 456 | | |
|---|
| 457 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 458 | | |
|---|
| 459 | | ``USStateField`` |
|---|
| 460 | | A two-letter U.S. state abbreviation. |
|---|
| 461 | | |
|---|
| 462 | | The admin represents this as an ``<input type="text">`` (a single-line input). |
|---|
| 463 | | |
|---|
| 464 | | ``XMLField`` |
|---|
| 465 | | A ``TextField`` that checks that the value is valid XML that matches a |
|---|
| 466 | | given schema. Takes one required argument, ``schema_path``, which is the |
|---|
| 467 | | filesystem path to a RelaxNG_ schema against which to validate the field. |
|---|
| 468 | | |
|---|
| 469 | | .. _RelaxNG: http://www.relaxng.org/ |
|---|
| | 591 | Verbose field names |
|---|
| | 592 | ------------------- |
|---|
| | 593 | |
|---|
| | 594 | Each field type, except for ``ForeignKey``, ``ManyToManyField`` and |
|---|
| | 595 | ``OneToOneField``, takes an optional first positional argument -- a |
|---|
| | 596 | verbose name. If the verbose name isn't given, Django will automatically create |
|---|
| | 597 | it using the field's attribute name, converting underscores to spaces. |
|---|
| | 598 | |
|---|
| | 599 | In this example, the verbose name is ``"Person's first name"``:: |
|---|
| | 600 | |
|---|
| | 601 | first_name = models.CharField("Person's first name", maxlength=30) |
|---|
| | 602 | |
|---|
| | 603 | In this example, the verbose name is ``"first name"``:: |
|---|
| | 604 | |
|---|
| | 605 | first_name = models.CharField(maxlength=30) |
|---|
| | 606 | |
|---|
| | 607 | ``ForeignKey``, ``ManyToManyField`` and ``OneToOneField`` require the first |
|---|
| | 608 | argument to be a model class, so use the ``verbose_name`` keyword argument:: |
|---|
| | 609 | |
|---|
| | 610 | poll = models.ForeignKey(Poll, verbose_name="the related poll") |
|---|
| | 611 | sites = models.ManyToManyField(Site, verbose_name="list of sites") |
|---|
| | 612 | place = models.OneToOneField(Place, verbose_name="related place") |
|---|
| | 613 | |
|---|
| | 614 | Convention is not to capitalize the first letter of the ``verbose_name``. |
|---|
| | 615 | Django will automatically capitalize the first letter where it needs to. |
|---|