Nigel Geroge의 "Mastering Django"를 공부하며 하이라이트한 내용을 정리해두었다.
app
to INSTALLED_APPS
INSTALLED_APPS
tells Django which apps are activated for a given project.python manage.py check
check
command runs the Django system check framework-a set of static checks for validating Django projects.python manage.py makemigrations <app>
python manage.py sqlmigrate <app> 0001
sqlmigrate
command prints output to the screen so you can see what SQL Djnago would execute if you asked it.python manage.py migrate <app>
migrate
commnad will take your latest migration file and update your database shema automaticallyobjects.create()
method.__str__()
method objects
attibute is called a manager. The managers take care of all table-level operations on data including data lookup.__contatins
part gets translated by Django into a SQL LIKE
statement:>>> Publisher.objects.filter(name__contains="press")
<QuerySet [<Publisher: Apress>]>
get()
method returns only a single object instead of a list.>>> Publisher.objects.get(name='Apress')
<Publisher: Apress>
DoesNotExist
exception is an attribute of the model's class-Publisher.DoesNotExist
.>>> try:
... p = Publisher.objects.get(name="KOREA")
... except Publisher.DoesNotExist:
... print("KOREA isn't in the database yet.")
... else:
... print("KOREA is in the database.")
...
KOREA isn't in the database yet.
order_by()
method:>>> Publisher.objects.order_by("name")
<QuerySet [<Publisher: Apress>, <Publisher: O'Reilly>]>
# multiple arguments
>>> Publisher.objects.order_by("state_province", "address")
<QuerySet [<Publisher: Apress>, <Publisher: O'Reilly>]>
# reverse ordering
>>> Publisher.objects.order_by("-name")
<QuerySet [<Publisher: O'Reilly>, <Publisher: Apress>]>
Meta
class.class Publisher(models.Model):
class Meta:
ordering = ['name']
Meta
class is used on any model to specify various model-specific options.>>> Publisher.objects.filter(country='U.S.A.').order_by("-name")
<QuerySet [<Publisher: O'Reilly>, <Publisher: Apress>]>
save()
method sets all of the column values, use the update()
method on QuerySet
objects in order to change only the column you need to change.>>> Publisher.objects.filter(id=1).update(name='Bpress')
1
>>> Publisher.objects.all().update(country='USA')
2
update()
method has a reutrn value-an integer representing how many records changed.# proejct_dir/app_dir/admin.py
from django.contrib import admin
from .models import Publisher
admin.site.register(Publisher)
blank=True
class Author(models.Model):
email = models.EmailField(blank=True)
null=True
and blank=True
NULL
could mean "unknown", or "invalid" ,and a value of NULL
is different than an empty string.CREATE TABLE
statements add an explicit NOT NULL
to each column definition.NULL
is the only the way to specify an empty value.class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher, on_delete=models.PROTECT)
publication_date = models.DateField(blank=True, null=True)
filter_horizontal
for any ManyToManyField
that has more than ten items.class BookAdmin(admin.ModelAdmin):
filter_horizontal = ('authors',)
request.META
is a Python dictionary containing all avaliable HTTP headers for the given request. When use it, use a try/except
clause or the get()
method to handle KeyError
.def ua_display(request):
ua = request.META.get("HTTP_USER_AGENT", "unknown")
return HttpResponse("Your browser is % s" % ua)
def ua_display(request):
try:
ua = request.META["HTTP_USER_AGENT"]
except KeyError:
ua = "unknown"
return HttpResponse("Your browser is % s" % ua)
testing은 내용이 많아서 여기서 정리한다.