Skip to content

Commit 44aef72

Browse files
author
Noah Lloyd-Edelman
committed
More notes. Finished the model section of the documentation.
1 parent 7d5e0f1 commit 44aef72

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

django.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ admin.site.register(MN, MNAdmin)
9898

9999
https://docs.djangoproject.com/en/1.11/topics/db/examples/
100100

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+
101105
````
102106
from django.db import models
103107
@@ -131,12 +135,14 @@ class ClassName(models.Model):
131135
many_to_many = models.ManyToManyField(<target_model>)
132136
many_to_many = models.ManyToManyField(
133137
<target_model>,
134-
through='Intermediate table',
138+
through='Intermediate table', # can make your own if you would like to add extra data
135139
)
136140
137141
class Meta:
138-
ordering = ['var']
142+
ordering = ['-var']
143+
verbose_name = 'thing'
139144
verbose_name_plural = 'things'
145+
unique_together = (('dog', 'cat'),)
140146
141147
__str__(self)
142148
return self.var
@@ -162,14 +168,66 @@ class AbstractChildModel(AbstractBaseModel):
162168

163169
### urls.py
164170

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+
165180
````
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
167205
168206
from . import views
169207
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'
170215
urlpatterns = [
171216
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+
]))
173231
]
174232
````
175233

0 commit comments

Comments
 (0)