|
| 1 | +title: django.db.models CharField Example Code |
| 2 | +category: page |
| 3 | +slug: django-db-models-charfield-examples |
| 4 | +sortorder: 50101 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.db.models CharField |
| 7 | +meta: Python code examples for the CharField class used in the Django ORM, found within the django.db.models module of the Django project. |
| 8 | + |
| 9 | + |
| 10 | +[CharField](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py) |
| 11 | +is a commonly-defined field used as an attribute to reference a |
| 12 | +text-based [database](/databases.html) column when defining |
| 13 | +[Model](/django-db-models-model-examples.html) classes with |
| 14 | +the [Django ORM](/django-orm.html). |
| 15 | + |
| 16 | +The [Django](/django.html) project has wonderful documentation for |
| 17 | +[CharField](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.CharField) |
| 18 | +and all of the other column fields. |
| 19 | + |
| 20 | +Note that `CharField` is defined within the |
| 21 | +[django.db.models.fields](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py) |
| 22 | +module but is typically referenced from |
| 23 | +[django.db.models](https://github.com/django/django/tree/master/django/db/models) |
| 24 | +rather than including the `fields` module reference. |
| 25 | + |
| 26 | + |
| 27 | +## Example 1 from django-smithy |
| 28 | +[django-smithy](https://github.com/jamiecounsell/django-smithy) is |
| 29 | +a code library for [Django](/django.html) that allows users to send |
| 30 | +HTTP requests from the Django admin user interface. The code for |
| 31 | +the project is open source under the |
| 32 | +[MIT license](https://github.com/jamiecounsell/django-smithy/blob/master/LICENSE). |
| 33 | + |
| 34 | +[**django-smithy / smithy / models.py**](https://github.com/jamiecounsell/django-smithy/blob/master/smithy/models.py) |
| 35 | + |
| 36 | +```python |
| 37 | +# -*- coding: utf-8 -*- |
| 38 | + |
| 39 | +~~from django.db import models |
| 40 | +from django.db.models.signals import post_save |
| 41 | +from django.dispatch import receiver |
| 42 | +from requests import Request as HTTPRequest, Session |
| 43 | +from requests.cookies import create_cookie, RequestsCookieJar |
| 44 | +from urllib.parse import parse_qs, urlparse, urlencode |
| 45 | +from requests_toolbelt.utils import dump |
| 46 | + |
| 47 | +from model_utils.models import TimeStampedModel |
| 48 | + |
| 49 | +from smithy.helpers import render_with_context, parse_dump_result |
| 50 | + |
| 51 | + |
| 52 | +class NameValueModel(TimeStampedModel): |
| 53 | +~~ name = models.CharField(max_length = 200) |
| 54 | + value = models.TextField(blank = True) |
| 55 | + |
| 56 | + def __str__(self): |
| 57 | + return self.name |
| 58 | + |
| 59 | + class Meta: |
| 60 | + abstract = True |
| 61 | + |
| 62 | + |
| 63 | +class Request(TimeStampedModel): |
| 64 | + """ |
| 65 | + A base model shared by RequestBlueprint and |
| 66 | + RequestRecord. Used solely to reduce |
| 67 | + """ |
| 68 | + METHODS = ( |
| 69 | + ('GET', 'GET'), |
| 70 | + ('POST', 'POST'), |
| 71 | + ('PUT', 'PUT'), |
| 72 | + ('DELETE', 'DELETE'), |
| 73 | + ('OPTIONS', 'OPTIONS'), |
| 74 | + ('HEAD', 'HEAD'), |
| 75 | + ) |
| 76 | + BODY_TYPES = ( |
| 77 | + ('', 'Other'), |
| 78 | + ('application/x-www-form-urlencoded', 'x-www-form-urlencoded'), |
| 79 | + ('application/json', 'JSON'), |
| 80 | + ('text/plain', 'Text'), |
| 81 | + ('application/javascript', 'JavaScript'), |
| 82 | + ('application/xml', 'XML (application/xml)'), |
| 83 | + ('text/xml', 'XML (text/xml)'), |
| 84 | + ('text/html', 'HTML'), |
| 85 | + ) |
| 86 | +~~ method = models.CharField( |
| 87 | +~~ max_length = 7, choices = METHODS, |
| 88 | +~~ blank = False, null = False) |
| 89 | +~~ name = models.CharField(max_length = 500, blank = False) |
| 90 | +~~ url = models.CharField(max_length = 2083) |
| 91 | + body = models.TextField(blank = True) |
| 92 | +~~ content_type = models.CharField( |
| 93 | +~~ default = BODY_TYPES[0][0], |
| 94 | +~~ blank = True, null = True, |
| 95 | +~~ max_length = 100, choices = BODY_TYPES) |
| 96 | + |
| 97 | + def __str__(self): |
| 98 | + if self.name: |
| 99 | + return self.name |
| 100 | + return "{} {}".format( |
| 101 | + self.method, |
| 102 | + self.url, |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +## source file continues from here without further CharField examples |
| 107 | +``` |
0 commit comments