Skip to content

Commit 7e768af

Browse files
committed
add django charfield examples page
1 parent f0eb29b commit 7e768af

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

content/pages/examples/django/django-code-examples.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ Example code found in the AuditLog project:
3939
* [django.utils.html format_html](/django-utils-html-format-html-examples.html)
4040

4141

42+
### dccnsys
43+
[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
44+
system built with [Django](/django.html). The code is open source under the
45+
[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
46+
47+
dccnsys is shown on the following code example pages:
48+
49+
* [django.urls.path](/django-urls-path-examples.html)
50+
51+
4252
### django-allauth
4353
[django-allauth](https://github.com/pennersr/django-allauth)
4454
([project website](https://www.intenct.nl/projects/django-allauth/)) is a
@@ -147,6 +157,7 @@ the project is open source under the
147157
Code examples from django-smithy are shown on the following pages:
148158

149159
* [django.utils timezone](/django-utils-timezone-examples.html)
160+
* [django.db.models CharField](/django-db-models-charfield-examples.html)
150161

151162

152163
### gadget-board
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
```

content/pages/examples/django/django-db-models-model.markdown

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ changes to [Django](/django.html) models. The source code is available
2020
under the
2121
[BSD 3 "New" license](https://github.com/vvangelovski/django-audit-log/blob/master/LICENSE.txt).
2222

23-
[**django-audit-log/audit-log/models/__init__.py**](https://github.com/vvangelovski/django-audit-log/blob/master/audit_log/models/__init__.py)
23+
[**django-audit-log / audit-log / models / __init__.py**](https://github.com/vvangelovski/django-audit-log/blob/master/audit_log/models/__init__.py)
2424

2525
```python
2626
# __init__.py
@@ -54,7 +54,7 @@ content management system (CMS)
5454
for use with Django web apps that is open sourced under the
5555
[BSD 3-Clause "New" License](https://github.com/divio/django-cms/blob/develop/LICENSE).
5656

57-
[**django-cms/cms/models/permisssionmodels.py**](https://github.com/divio/django-cms/blob/develop/cms/models/pagemodel.py)
57+
[**django-cms / cms / models / permisssionmodels.py**](https://github.com/divio/django-cms/blob/develop/cms/models/pagemodel.py)
5858

5959
```python
6060
# -*- coding: utf-8 -*-
@@ -133,7 +133,8 @@ ACCESS_CHOICES = (
133133
~~ can_change = models.BooleanField(_("can edit"), default=True)
134134
~~ can_add = models.BooleanField(_("can add"), default=True)
135135
~~ can_delete = models.BooleanField(_("can delete"), default=True)
136-
~~ can_change_advanced_settings = models.BooleanField(_("can change advanced settings"),
136+
~~ can_change_advanced_settings = models.BooleanField(_(\
137+
~~ "can change advanced settings"),
137138
default=False)
138139
~~ can_publish = models.BooleanField(_("can publish"), default=True)
139140
~~ can_change_permissions = models.BooleanField(_("can change permissions"),
@@ -247,7 +248,7 @@ ACCESS_CHOICES = (
247248
system built with [Django](/django.html). The code is open source under the
248249
[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
249250

250-
[**dccnconf/wwwdccn/conferences/models.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/conferences/models.py)
251+
[**dccnconf / wwwdccn / conferences / models.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/conferences/models.py)
251252

252253
```python
253254
from django.conf import settings
@@ -442,7 +443,7 @@ from django_countries.fields import CountryField
442443
[Django](/django.html)-based CMS with open source code under the
443444
[BSD 2-Clause "Simplified" License](https://github.com/stephenmcd/mezzanine/blob/master/LICENSE).
444445

445-
[**mezzanine/mezzanine/core/models.py**](https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/models.py)
446+
[**mezzanine / mezzanine / core / models.py**](https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/models.py)
446447

447448
```python
448449
from __future__ import unicode_literals

0 commit comments

Comments
 (0)