@@ -98,6 +98,10 @@ admin.site.register(MN, MNAdmin)
98
98
99
99
https://docs.djangoproject.com/en/1.11/topics/db/examples/
100
100
101
+ - Abstract model = a place to store a common field among many tables. Does not actually create a table
102
+ - ManyToManyField = A pizza can have many toppings, and a topping may be on many pizzas
103
+ - Extends something to another model
104
+
101
105
````
102
106
from django.db import models
103
107
@@ -131,12 +135,14 @@ class ClassName(models.Model):
131
135
many_to_many = models.ManyToManyField(<target_model>)
132
136
many_to_many = models.ManyToManyField(
133
137
<target_model>,
134
- through='Intermediate table',
138
+ through='Intermediate table', # can make your own if you would like to add extra data
135
139
)
136
140
137
141
class Meta:
138
- ordering = ['var']
142
+ ordering = ['-var']
143
+ verbose_name = 'thing'
139
144
verbose_name_plural = 'things'
145
+ unique_together = (('dog', 'cat'),)
140
146
141
147
__str__(self)
142
148
return self.var
@@ -162,14 +168,66 @@ class AbstractChildModel(AbstractBaseModel):
162
168
163
169
### urls.py
164
170
171
+ * url contents are always delivered as strings in the following format ` (request, var, var, ...) `
172
+ * ` (?P(<name>pattern)) ` = named groups
173
+ * capture groups can be nested
174
+ * ` ?: ` = non-captured group
175
+ * name-spaced urls can be a best practice ` <app>:<url> ` when clashing url names. For example ` admin:index ` or ` sports:polls:index ` .
176
+
177
+
178
+ Reversing URLS in Python and Templates
179
+
165
180
````
166
- from django.conf.urls import url
181
+ from django.urls import reverse
182
+
183
+ year = 2006
184
+ reverse('url-name', args=(year,))
185
+ reverse('polls:index', current_app=self.request.resolver_match.namespace)
186
+
187
+
188
+ In templates, urls called via: {% url 'url-name' <vars> %}
189
+ For example: {% url 'stuff' 2012 %} or {% url 'polls:index' %}
190
+ ````
191
+
192
+ The app_name variable or the following method can be used for namespacing
193
+
194
+ ````
195
+ polls_patterns = ([
196
+ url(r'^$', views.IndexView.as_view(), name='index'),
197
+ url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
198
+ ], 'polls')
199
+ ````
200
+
201
+ URL examples
202
+
203
+ ````
204
+ from django.conf.urls import url, include
167
205
168
206
from . import views
169
207
208
+ # additional urls that can be added
209
+ extra_patterns = [
210
+ url(r'^stuff/$', stuff_views.stuff),
211
+ ]
212
+
213
+ # url examples
214
+ app_name = 'polls'
170
215
urlpatterns = [
171
216
url(r'^$', views.index, name='index'),
172
- url(r'(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='details'),
217
+ url('^stuff/', include(extra_patterns)),
218
+ url(r'^articles/([0-9]{4})/$', views.article, name='stuff'), # capture values by surrounding in parentheses
219
+ url(r'^articles/(?P<year>[0-9]{$4})/$', views.article, name='stuff'), # same as the above expression but with a named group
220
+ url(r'contract/', include('django_website.contact.urls')),
221
+ url(r'comments/(?:page-(?P<page_number>\d+)/)?$', comments), # returns page_number=2
222
+ ]
223
+
224
+ # used for simplifing urls with the same base url
225
+ urlpatterns = [
226
+ url(r'^(?P<page_slug>[w\-]+)-(?P<page_id>\w+)/, include([
227
+ url(r'^history/$', views.history),
228
+ url(r'^edit/$', views.edit),
229
+ url(r'^discuss/$', views.discuss),
230
+ ]))
173
231
]
174
232
````
175
233
0 commit comments