Skip to content

Commit a7cdd4c

Browse files
committed
Add more content in designing models
1 parent a136dd9 commit a7cdd4c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- [Security](#security)
1010
- [Deployments](#deployments)
1111
- [Authentication](#authentication)
12+
- [Autherization](#authorization)
13+
- [Views](#views)
14+
- [Urls](#urls)
1215

1316
## Designing Models
1417

@@ -107,6 +110,39 @@
107110

108111
- Any changes in your models requires you to run `python manage.py makemigrations` and `python manage.py migrate`
109112

113+
- Models should include all relevant domain/business logic as it's property or methods. For example for a player you might want to check if he is a captain or not:
114+
115+
```python
116+
class Player(models.Model):
117+
name = models.CharField(max_length=100)
118+
119+
def is_captain(self):
120+
if self is captain:
121+
return True
122+
return False
123+
```
124+
125+
- Blank and Null Fields:
126+
127+
- `Null` is database-related. Defines if a given database column will accept null values or not.
128+
129+
- `Blank` is validation-related. It will be used during forms validation, when calling form.is_valid().
130+
131+
- Do not use `null=True` for text/char based field. Otherwise, you will end up having two possible values for "no data" that is: `None` and `an empty` string. Having two possible values for "no data" is redundant The Django convention is to use the empty string, not NULL.
132+
133+
```python
134+
class Player(models.Model):
135+
name = models.CharField(max_length=100)
136+
137+
# null=True is not required as `empty string` will be saved to the database.
138+
bio = models.TextField(blank=True)
139+
140+
# here you may add `null=True`
141+
birth_date = models.DateField(null=True, blank=True)
142+
```
143+
144+
- Do not use `null=True` or `blank=True` for `BooleanField`. It is better to specify default values for such fields. If you realise that the field can remain empty, you need `NullBooleanField`.
145+
110146
## Caching
111147

112148
- ToDo

0 commit comments

Comments
 (0)