Added Department model, updated WidgetUser model and views

parent 9ca54cbd
No preview for this file type
from django.contrib import admin from django.contrib import admin
from .models import WidgetUser from .models import WidgetUser, Department
# Register your models here. # Register your models here.
class WidgetUserAdmin(admin.ModelAdmin): class WidgetUserAdmin(admin.ModelAdmin):
model = WidgetUser model = WidgetUser
admin.site.register(WidgetUser, WidgetUserAdmin) class DepartmentAdmin(admin.ModelAdmin):
\ No newline at end of file model = Department
admin.site.register(WidgetUser, WidgetUserAdmin)
admin.site.register(Department, DepartmentAdmin)
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-04-04 12:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Department',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('dept_name', models.CharField(max_length=50)),
('home_unit', models.CharField(max_length=50)),
],
),
migrations.AddField(
model_name='widgetuser',
name='email',
field=models.EmailField(default=1, max_length=254),
preserve_default=False,
),
migrations.AddField(
model_name='widgetuser',
name='id_num',
field=models.CharField(default=1, max_length=7),
preserve_default=False,
),
]
# Generated by Django 4.0.3 on 2022-04-04 12:47
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0002_department_widgetuser_email_widgetuser_id_num'),
]
operations = [
migrations.AddField(
model_name='widgetuser',
name='department',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.department'),
preserve_default=False,
),
]
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Department(models.Model):
dept_name = models.CharField(max_length=50)
home_unit = models.CharField(max_length=50)
@property
def dept_info(self):
return '{}, {}'.format(self.dept_name, self.home_unit)
class WidgetUser(models.Model): class WidgetUser(models.Model):
first_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50)
middle_name = models.CharField(max_length=50) middle_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
\ No newline at end of file
id_num = models.CharField(max_length=7)
email = models.EmailField()
department = models.ForeignKey(Department, on_delete = models.CASCADE)
@property
def user_info(self):
user = '<br>{}, {} {}: '.format(self.last_name, self.first_name, self.middle_name)
user += '{}, {}, {}'.format(self.id_num, self.email, self.department.dept_info)
return user
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
from .models import WidgetUser
def index(request): def index(request):
return HttpResponse('Welcome to Widget!') def user_list():
final_list = ''
for u in range(len(WidgetUser.objects.all())):
final_list += '{}'.format(WidgetUser.objects.get(pk=u+1).user_info)
return final_list
html = f'''
<html>
<body>
<header><h1>WIDGET USERS:</h2><header>
<main>
<p>{user_list()}</p>
</main>
</body>
</html>
'''
return HttpResponse(html)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment