Add User model field and Type and Post models

This commit is contained in:
Jean Lionel Ndabaga 2024-03-08 16:52:06 +02:00
parent 1a1cebd94e
commit c7efd64b96
5 changed files with 106 additions and 1 deletions

View file

@ -0,0 +1,53 @@
# Generated by Django 5.0.3 on 2024-03-08 14:50
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('email', models.EmailField(max_length=254, unique=True)),
('username', models.CharField(max_length=100, unique=True)),
('date_joined', models.DateTimeField(auto_now_add=True)),
('last_login', models.DateTimeField(auto_now=True)),
('is_admin', models.BooleanField(default=False)),
('is_staff', models.BooleanField(default=False)),
('is_active', models.BooleanField(default=True)),
('is_superuser', models.BooleanField(default=False)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='Type',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to='images/')),
('title', models.CharField(max_length=100)),
('content', models.TextField()),
('date_posted', models.DateTimeField(auto_now_add=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('post_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.type')),
],
),
]

View file

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractBaseUser from django.contrib.auth.models import AbstractBaseUser
from django.conf import settings
class User(AbstractBaseUser): class User(AbstractBaseUser):
@ -12,6 +13,8 @@ class User(AbstractBaseUser):
is_active = models.BooleanField(default=True) is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
def __str__(self): def __str__(self):
return f'{self.username} - {self.email}' return f'{self.username} - {self.email}'
@ -20,3 +23,23 @@ class User(AbstractBaseUser):
def has_module_perms(self, app_label): def has_module_perms(self, app_label):
return True return True
class Type(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Post(models.Model):
image = models.ImageField(upload_to='images/', null=True, blank=True)
title = models.CharField(max_length=100)
content = models.TextField()
post_type = models.ForeignKey(Type, on_delete=models.CASCADE)
date_posted = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
def __str__(self):
return self.title

5
api/urls.py Normal file
View file

@ -0,0 +1,5 @@
from django.urls import path
urlpatterns = [
]

View file

@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.0/ref/settings/
""" """
from pathlib import Path from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
@ -37,6 +38,8 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'api',
'rest_framework',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -119,6 +122,10 @@ USE_TZ = True
STATIC_URL = 'static/' STATIC_URL = 'static/'
# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

View file

@ -15,8 +15,25 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('api/', include('api.urls')),
] ]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
"""
#### Steps For Creating API ####
Step 1: Create Model (For Creating database )
Step 2: Create Serializer (For converting model to JSON)
Step 3: Create View (For creating API)
Step 4: Create URL (For mapping the API / Creating the endpoint)
"""