Skip to content

Commit cd6fb7a

Browse files
martin056RadoRado
authored andcommitted
Add admin for easier setup
1 parent 7098828 commit cd6fb7a

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

config/django/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
'django_celery_results',
4949
'django_celery_beat',
5050
'django_filters',
51-
'corsheaders'
51+
'corsheaders',
52+
'django_extensions',
5253
]
5354

5455
INSTALLED_APPS = [

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ whitenoise==5.3.0
1111

1212
django-filter==21.1
1313
django-cors-headers==3.10.1
14+
django-extensions==3.1.5
Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11
from django.contrib import admin
22

3-
# Register your models here.
3+
from styleguide_example.test_examples.models import (
4+
School,
5+
SchoolClass,
6+
Student,
7+
Roster
8+
)
9+
10+
11+
@admin.register(School)
12+
class SchoolAdmin(admin.ModelAdmin):
13+
list_display = ('name', 'slug', )
14+
15+
16+
@admin.register(Roster)
17+
class RosterAdmin(admin.ModelAdmin):
18+
list_display = ('student', 'school_class', 'start_date', 'end_date', 'active', )
19+
list_filter = ('student', 'school_class')
20+
21+
22+
class RosterStudentInline(admin.TabularInline):
23+
model = Roster
24+
fields = ('school_class', 'start_date', 'end_date', 'active', 'deactivated_at', )
25+
extra = 1
26+
27+
28+
class RosterSchoolClassInline(admin.TabularInline):
29+
model = Roster
30+
fields = ('student', 'start_date', 'end_date', 'active', 'deactivated_at', )
31+
extra = 1
32+
33+
34+
@admin.register(SchoolClass)
35+
class SchoolClassAdmin(admin.ModelAdmin):
36+
list_display = ('name', 'short_name', 'slug', 'school', 'start_date', 'end_date', )
37+
list_filter = ('school', )
38+
inlines = (RosterSchoolClassInline, )
39+
40+
41+
@admin.register(Student)
42+
class StudentAdmin(admin.ModelAdmin):
43+
list_display = ('email', 'identifier', 'school', )
44+
list_filter = ('school', )
45+
inlines = (RosterStudentInline, )

styleguide_example/test_examples/migrations/0001_initial.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.9 on 2021-11-18 09:46
1+
# Generated by Django 3.2.9 on 2021-11-18 10:11
22

33
from django.db import migrations, models
44
import django.db.models.deletion
@@ -39,12 +39,12 @@ class Migration(migrations.Migration):
3939
fields=[
4040
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
4141
('name', models.CharField(max_length=255)),
42+
('short_name', models.CharField(max_length=50)),
4243
('slug', models.SlugField(max_length=255)),
44+
('start_date', models.DateField()),
45+
('end_date', models.DateField()),
4346
('school', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='school_classes', to='test_examples.school')),
4447
],
45-
options={
46-
'unique_together': {('slug', 'school')},
47-
},
4848
),
4949
migrations.CreateModel(
5050
name='Roster',
@@ -53,11 +53,15 @@ class Migration(migrations.Migration):
5353
('start_date', models.DateField()),
5454
('end_date', models.DateField()),
5555
('active', models.BooleanField(default=True)),
56-
('deactivated_at', models.DateField(null=True)),
56+
('deactivated_at', models.DateField(blank=True, null=True)),
5757
('school_class', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rosters', to='test_examples.schoolclass')),
5858
('student', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rosters', to='test_examples.student')),
5959
],
6060
),
61+
migrations.AddConstraint(
62+
model_name='schoolclass',
63+
constraint=models.CheckConstraint(check=models.Q(('start_date__lt', django.db.models.expressions.F('end_date'))), name='school_class_start_before_end'),
64+
),
6165
migrations.AddConstraint(
6266
model_name='roster',
6367
constraint=models.CheckConstraint(check=models.Q(('start_date__lt', django.db.models.expressions.F('end_date'))), name='roster_start_before_end'),

styleguide_example/test_examples/models.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@ def __str__(self):
2929

3030
class SchoolClass(models.Model):
3131
name = models.CharField(max_length=255)
32+
short_name = models.CharField(max_length=50)
3233
slug = models.SlugField(max_length=255)
3334
school = models.ForeignKey(School, related_name='school_classes', on_delete=models.CASCADE)
3435

36+
start_date = models.DateField()
37+
end_date = models.DateField()
38+
3539
class Meta:
36-
unique_together = (
37-
('slug', 'school'),
38-
)
40+
constraints = [
41+
models.CheckConstraint(
42+
name="school_class_start_before_end",
43+
check=Q(start_date__lt=F("end_date"))
44+
)
45+
]
46+
47+
def __str__(self):
48+
return f'{self.name} in {self.school}'
3949

4050

4151
class Roster(models.Model):
@@ -46,7 +56,7 @@ class Roster(models.Model):
4656
end_date = models.DateField()
4757

4858
active = models.BooleanField(default=True)
49-
deactivated_at = models.DateField(null=True)
59+
deactivated_at = models.DateField(null=True, blank=True)
5060

5161
class Meta:
5262
constraints = [

0 commit comments

Comments
 (0)