Database Setup
Database Setup
ENGINE –
'django.db.backends.sqlite3', or
'django.db.backends.postgresql', or
'django.db.backends.mysql', or
'django.db.backends.oracle'. etc.
NAME –
The name of your database. If you’re using SQLite, the database will
be a file on your computer; in that case, NAME should be the full
absolute path, including filename, of that file.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myproject',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306'', }}
Creating models
Now we’ll define your models – essentially, your database layout,
with additional metadata.
Example
INSTALLED_APPS = [
..........,
'app'
]
Username: admin
But where’s our app? It’s not displayed on the admin index page.
app/admin.py
admin.site.register(Student)
admin.site.register(Student_Details)
Example:
b.save()
>>> b5.save()
Retrieving objects
To retrieve objects from your database, construct a QuerySet via
a Manager on your model class.
>>> Blog.objects
>>> b.objects
Traceback:
...
filter(**kwargs)
exclude(**kwargs)
For example, to get a QuerySet of blog entries from the year 2006,
use filter() like so:
Entry.objects.filter(pub_date__year=2006)
If you know there is only one object that matches your query, you
can use the get() method on aManager which returns the object
directly:
>>> one_entry = Entry.objects.get(pk=1)
Limiting QuerySets
Use a subset of Python’s array-slicing syntax to limit
your QuerySet to a certain number of results. This is the equivalent
of SQL’s LIMIT and OFFSET clauses.
>>> Entry.objects.all()[:5]
>>> Entry.objects.all()[5:10]
Field lookups
Field lookups are how you specify the meat of an SQL WHERE clause.
They’re specified as keyword arguments to
the QuerySet methods filter(), exclude() and get().
For example:
>>> Entry.objects.filter(pub_date__lte='2006-01-01')
roughly SQL:
exact
An “exact” match. For example:
>>> Blog.objects.get(id__exact=14)
>>> Blog.objects.get(id=14)
iexact
A case-insensitive match. So, the query:
contains
Case-sensitive containment test. For example:
Entry.objects.get(headline__contains='Lennon')
https://docs.djangoproject.com/en/2.0/intro/tutorial03/