<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>django | Alex Hildebrandt</title><link>https://alexhildebrandt.de/category/django/</link><atom:link href="https://alexhildebrandt.de/category/django/index.xml" rel="self" type="application/rss+xml"/><description>django</description><generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><lastBuildDate>Wed, 20 May 2020 00:00:00 +0000</lastBuildDate><image><url>https://alexhildebrandt.de/images/icon_hu4c6883ab8262edd297260ea54e77bd73_39834_512x512_fill_lanczos_center_2.png</url><title>django</title><link>https://alexhildebrandt.de/category/django/</link></image><item><title>Django Database</title><link>https://alexhildebrandt.de/post/django-database/</link><pubDate>Wed, 20 May 2020 00:00:00 +0000</pubDate><guid>https://alexhildebrandt.de/post/django-database/</guid><description>
&lt;div id="handling-databases-in-django" class="section level2">
&lt;h2>Handling databases in Django&lt;/h2>
&lt;p>When starting a project sqlite is preinstalled. This database is in development sufficient. When going to production it is recommended to use postgresDB.&lt;/p>
&lt;p>In django you can create new databases within the models.py of your app. Each app will have its own databases. The code for a models entry could look like this:&lt;/p>
&lt;pre>&lt;code>from django.db import models
from django.utils impor timezone
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)&lt;/code>&lt;/pre>
&lt;p>Here a database wiht the name &lt;strong>&lt;em>Post&lt;/em>&lt;/strong> is created. This table has different columns which are defined by the Fields parameter. You need to run &lt;em>python3 manage.py makemigrations&lt;/em> to get migration of the model. In the migrations directory now a file is created which holds all the informations. To see what sql is doing behind the scene you can type:&lt;/p>
&lt;pre>&lt;code>python3 manage.py sqlmigrate blog 0001&lt;/code>&lt;/pre>
&lt;p>&lt;strong>blog&lt;/strong> is the app name and &lt;strong>0001&lt;/strong> is the created filename in the migrations folder. The output is what you have to write in sql.&lt;/p>
&lt;p>Now you need to make the database available with:&lt;/p>
&lt;pre>&lt;code>python3 manage.py migrate&lt;/code>&lt;/pre>
&lt;p>If you want to use this database on a site you need to import it in the views.py file:&lt;/p>
&lt;pre>&lt;code>from django.shortcuts import render
from .models import Post
def home(request):
context = {
&amp;#39;posts&amp;#39;: Post.objects.all()
}
return render(request, &amp;#39;blog/home.html&amp;#39;, context)
def about(request):
return render(request, &amp;#39;blog/about.html&amp;#39;, {&amp;#39;title&amp;#39;: &amp;#39;About&amp;#39;})
&lt;/code>&lt;/pre>
&lt;p>If you want to make your models / databases available in the backend, you need to register the models in the admin.py file of your app:&lt;/p>
&lt;pre>&lt;code>from django.contrib import admin
from .models import Post
admin.site.register(Post)&lt;/code>&lt;/pre>
&lt;p>In the backend you can easy handle the models/databases. Create new entries and change metadata information.&lt;/p>
&lt;hr />
&lt;/div></description></item></channel></rss>