Skip to content

Commit 9a5ec45

Browse files
committed
Added patterns. Fixed some typos.
1 parent 6bd7851 commit 9a5ec45

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

source/misc.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,102 @@ If slugs do not make sense, instead use a CRC algorithm.::
9797
import zlib
9898
#Use permalink in real case
9999
return '/customer/%s/' % zlib.crc32(self.pk)
100+
101+
Prefer ModelForm to Form
102+
--------------------------
103+
ModelForm already know the correct UI widgets for your underlying Models. In
104+
most of the cases ModelForm would suffice instead of Forms.
105+
106+
Some common scnarios
107+
108+
Hiding some fields from ModelForm which are needed for a DB save.
109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
111+
Eg, you want too create a profile for the logged in user.::
112+
113+
#in Forms.py
114+
class ProfileForm(forms.ModelForm):
115+
class Meta:
116+
model = Profile
117+
exclude =['user',]
118+
119+
#In Views:
120+
form = ProfileForm(request.POST)
121+
profile = form.save(commit = False)
122+
profile.user = request.user
123+
profile.save()
124+
125+
Or::
126+
127+
#Todo test this
128+
class ProfileForm(forms.ModelForm):
129+
class Meta:
130+
model = Profile
131+
exclude =['user',]
132+
def __init__(self, user, *args, **kwargs)
133+
self.user = user
134+
super(ProfileForm, self).__init__(*args, **kwargs)
135+
136+
def save(*args, **kwargs):
137+
self.instance.user = self.user
138+
super(ProfileForm, self).save(*args, **kwargs)
139+
140+
Saving multiple Objects in one form
141+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142+
143+
As::
144+
145+
class ProfileForm(forms.ModelForm):
146+
class Meta:
147+
model = Profile
148+
exclude =['user',]
149+
150+
class UserForm(forms.ModelForm):
151+
class Meta:
152+
model = User
153+
exclude =[...]
154+
155+
#in views.py
156+
userform = UserForm(request.POST)
157+
profileform = ProfileForm(request.POST)
158+
if userform.is_valid() and profileform.is_valid():
159+
#Only if both are valid together
160+
user = userform.save()
161+
profile = profileform.save(commit = False)
162+
profile.user = user
163+
profile.save()
164+
165+
{# In templates #}
166+
<form ...>
167+
{{ userform }}
168+
{{ profileform }}
169+
<input type="submit" />
170+
</form>
100171

101172

102173

174+
175+
Forms should know how to save themselves.
176+
---------------------------------------------
177+
if your forms is a forms.ModelForm, it already knows how to save its data. If you
178+
write a forms.Form, it should have a `.save()`. This keeps things symmetrical with
179+
`ModelForms`, and allows you to do::
180+
181+
#in views.py
182+
def view_func(request):
183+
if request.method == 'POST':
184+
form = FormClass(request.POST)
185+
if form.is_valid():
186+
obj = form.save()
187+
...
188+
...
189+
190+
Instead of::
191+
192+
if form.is_valid():
193+
#handle the saving in DB inside of views.
194+
195+
The `.save()` should return a Model Object
196+
103197

104198

source/models.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ do not want to display any object on your site which is unapproved(is_approved =
99
False in your Model).::
1010

1111
class ModelClassApprovedOnlyManager(models.Manager):
12-
self.get_query_set().filter(is_approved = True)
12+
def get_query_set():
13+
self.get_query_set().filter(is_approved = True)
1314

1415
class ModelClass(models.Model):
1516
...
@@ -154,6 +155,13 @@ One way to do so would be to override, `save` and `delete`.::
154155
Other option would be to attach listeners for `post_save` and `post_delete`.
155156
#TODO
156157

158+
159+
Abstract custom queries in Manager methods.
160+
----------------------------------------------
161+
162+
If you have some complex Sql query, not easily representable via Django ORM,
163+
you can write custom Sql. These should be abstracted as Manager methods.
164+
157165

158166

159167

source/workflow.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ south is propbably more popular.
1818

1919
Create various entries in your /etc/hosts mapped to localhost
2020
------------------------------------------------------------------
21-
While development you probably want varius users logged in to the site
21+
While development you probably want various users logged in to the site
2222
simulataneously. For example, while developing, I have one user logged in the admin, one normal
2323
user using the site. If both try to access the site from localhost, one will be
2424
logged out when other logs in.

0 commit comments

Comments
 (0)