Skip to content

Commit 7a06f56

Browse files
committed
add a couple of filefield django code examples
1 parent 68dd537 commit 7a06f56

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
title: django.db.models FileField Example Code
2+
category: page
3+
slug: django-db-models-filefield-examples
4+
sortorder: 50127
5+
toc: False
6+
sidebartitle: django.db.models FileField
7+
meta: Python code examples for the FileField class used in the Django ORM, found within the django.db.models module of the Django project.
8+
9+
10+
[FileField](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py)
11+
is a [Django ORM](/django-orm.html) field-to-column mapping for
12+
uploading files from the client to the [Django](/django.html)
13+
application.
14+
15+
`FileField` is defined within the
16+
[django.db.models.fields](https://github.com/django/django/blob/master/django/db/models/fields/__init__.py)
17+
module but is typically referenced from
18+
[django.db.models](https://github.com/django/django/tree/master/django/db/models)
19+
rather than including the `fields` module reference.
20+
21+
22+
## Example 1 from dccnsys
23+
[dccnsys](https://github.com/dccnconf/dccnsys) is a conference registration
24+
system built with [Django](/django.html). The code is open source under the
25+
[MIT license](https://github.com/dccnconf/dccnsys/blob/master/LICENSE).
26+
27+
[**dccnsys / wwwdccn / submissions / models.py**](https://github.com/dccnconf/dccnsys/blob/master/wwwdccn/submissions/models.py)
28+
29+
```python
30+
import os
31+
32+
from django.conf import settings
33+
from django.utils.translation import ugettext_lazy as _
34+
from django.contrib.auth import get_user_model
35+
~~from django.db import models
36+
37+
from conferences.models import Topic, SubmissionType, Conference
38+
39+
User = get_user_model()
40+
41+
42+
TITLE_MAX_LENGTH = 250
43+
ABSTRACT_MAX_LENGTH = 2500 # 250 words
44+
45+
46+
def get_review_manuscript_full_path(instance, filename):
47+
ext = filename.split('.')[-1]
48+
root = settings.MEDIA_PRIVATE_ROOT
49+
cpk = instance.conference.pk if instance.conference else 'unknown_conf'
50+
path = f'{root}/{cpk}/submissions'
51+
name = f'SID{instance.pk:05d}'
52+
return f'{path}/{name}.{ext}'
53+
54+
55+
class Submission(models.Model):
56+
SUBMITTED = 'SUBMIT'
57+
UNDER_REVIEW = 'REVIEW'
58+
REJECTED = 'REJECT'
59+
ACCEPTED = 'ACCEPT'
60+
IN_PRINT = 'PRINT'
61+
PUBLISHED = 'PUBLISH'
62+
63+
STATUS_CHOICE = (
64+
(SUBMITTED, _('Submitted')),
65+
(UNDER_REVIEW, _('Review')),
66+
(REJECTED, _('Rejected')),
67+
(ACCEPTED, _('Accepted')),
68+
(IN_PRINT, _('In-print')),
69+
(PUBLISHED, _('Published')),
70+
)
71+
72+
conference = models.ForeignKey(
73+
Conference,
74+
on_delete=models.SET_NULL,
75+
null=True,
76+
)
77+
78+
title = models.CharField(
79+
max_length=TITLE_MAX_LENGTH,
80+
default="",
81+
verbose_name=_('Title'),
82+
)
83+
84+
abstract = models.CharField(
85+
max_length=ABSTRACT_MAX_LENGTH,
86+
default="",
87+
verbose_name=_('Abstract'),
88+
)
89+
90+
topics = models.ManyToManyField(
91+
Topic,
92+
verbose_name=_('Topics'),
93+
)
94+
95+
stype = models.ForeignKey(
96+
SubmissionType,
97+
related_name='submissions',
98+
verbose_name=_('Submission type'),
99+
on_delete=models.SET_NULL,
100+
null=True,
101+
)
102+
103+
status = models.CharField(
104+
choices=STATUS_CHOICE,
105+
default='SUBMIT',
106+
max_length=10,
107+
)
108+
109+
~~ review_manuscript = models.FileField(
110+
~~ upload_to=get_review_manuscript_full_path,
111+
~~ blank=True,
112+
~~ )
113+
114+
115+
## ... source file continues with no further examples ...
116+
```
117+
118+
119+
## Example 2 from wagtail
120+
[wagtail](https://github.com/wagtail/wagtail)
121+
([project website](https://wagtail.io/)) is a fantastic
122+
[Django](/django.html)-based CMS with code that is open source
123+
under the
124+
[BSD 3-Clause "New" or "Revised" License](https://github.com/wagtail/wagtail/blob/master/LICENSE).
125+
126+
[**wagtail / wagtail / images / signal_handlers.py**](https://github.com/wagtail/wagtail/blob/master/wagtail/images/signal_handlers.py)
127+
128+
```python
129+
from django.conf import settings
130+
~~from django.db import transaction
131+
from django.db.models.signals import post_delete, pre_save
132+
133+
from wagtail.images import get_image_model
134+
135+
136+
~~def post_delete_file_cleanup(instance, **kwargs):
137+
~~ # Pass false so FileField doesn't save the model.
138+
~~ transaction.on_commit(lambda: instance.file.delete(False))
139+
140+
141+
def pre_save_image_feature_detection(instance, **kwargs):
142+
if getattr(settings, 'WAGTAILIMAGES_FEATURE_DETECTION_ENABLED', False):
143+
# Make sure the image doesn't already have a focal point
144+
if not instance.has_focal_point():
145+
# Set the focal point
146+
instance.set_focal_point(instance.get_suggested_focal_point())
147+
148+
149+
def register_signal_handlers():
150+
Image = get_image_model()
151+
Rendition = Image.get_rendition_model()
152+
153+
pre_save.connect(pre_save_image_feature_detection, sender=Image)
154+
post_delete.connect(post_delete_file_cleanup, sender=Image)
155+
post_delete.connect(post_delete_file_cleanup, sender=Rendition)
156+
157+
```
158+

content/pages/meta/00-change-log.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ on GitHub.
1616

1717
## 2019
1818
### October
19+
* Added new Django code example pages:
20+
* [django.db.models FileField](/django-db-models-filefield-examples.html)
21+
* Rewrote some parts of the [Python 2 or 3](/python-2-or-3.html) page to
22+
make it more clear that 3 is now the mandatory way to get started.
1923
* Updated [best videos](/best-python-videos.html) page with new EuroPython links.
2024

2125
### September

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ <h4 class="bp">django.db.models
252252
<a href="/django-db-models-charfield-examples.html">CharField</a>,
253253
<a href="/django-db-models-datefield-examples.html">DateField</a>,
254254
<a href="/django-db-models-datetimefield-examples.html">DateTimeField</a>,
255+
<a href="/django-db-models-filefield-examples.html">FileField</a>,
255256
<a href="/django-db-models-integerfield-examples.html">IntegerField</a>,
256257
<a href="/django-db-models-model-examples.html">Model</a>,
257258
<a href="/django-db-models-textfield-examples.html">TextField</a>

0 commit comments

Comments
 (0)