Django modules logic problem

I’m in the process of reading a Django-related book and browsing tutorials online. When I wanted to create my own project, I realized that I couldn’t find a seemingly simple solution to the problem. I would like the administrator to be able to:

  • Add a new physiotherapist from the administrator panel.
  • Create a template of hours for appointments (e.g., Standard: 07:00, 08:00, 09:00).
  • Generate appointment slots based on the selection of a physiotherapist, hour template, and added days (several).

Appointments should be a combination of days with hours and physiotherapists and should have a status. The problem is that I don’t know how to create a form that generates slots because it won’t be connected to any of the models, and I don’t know how to create a module and a template hour form because I don’t know how to create a composite key and how to add multiple hours to one template without creating a model of hours. If anyone has ideas on how to implement the logic above, I’m open to suggestions.

#Edit
I have come to the conclusion that I need to create a separate form for generating appointments. The data will populate the Appointment model. Now I’m wondering if I can redirect to the new form directly from the admin module

My models is:

models.py:

class HoursTemplate(models.Model):
    name = models.CharField(max_length=100)
    hours = models.ManyToManyField('Hour')

    def __str__(self):
        return self.name

class Hour(models.Model):
    hour = models.CharField(max_length=5)

    def __str__(self):
        return str(self.hour)

class Physiotherapist(models.Model):
    firstName = models.CharField(max_length=30)
    lastName = models.CharField(max_length=30)

    def __str__(self):
        return self.firstName + ' ' + self.lastName

class Appointment(models.Model):
    physiotherapist = models.ForeignKey('Physiotherapist', on_delete=models.CASCADE)
    hour = models.ForeignKey('Hour', on_delete=models.CASCADE)
    day = models.DateField()
    isFree = models.BooleanField(default=True)

Admin panel is
admin.py:

from django.contrib import admin
from django.db.models.query import QuerySet
from .models import HoursTemplate, Hour, Physiotherapist, Appointment
from datetime import date


class FutureAppointmentFilter(admin.SimpleListFilter):
    title = 'Appointment date'
    parameter_name = 'day'

    def lookups(self, request, model_admin):
        return[
            ('future', 'In the future'),
            ('past', 'In the past'),
        ]

    def queryset(self, request, queryset):
        if self.value() == 'future':
            return queryset.filter(day__gte=date.today())
        elif self.value() == 'past':
            return queryset.filter(day__lt=date.today())


admin.site.register(HoursTemplate)
admin.site.register(Hour)

@admin.register(Appointment) #dekorator
class AppointmentAdmin(admin.ModelAdmin):
    list_display = ['physiotherapist', 'hour', 'day', 'isFree']
    list_filter = [FutureAppointmentFilter]

@admin.register(Physiotherapist)
class PhystiotherapistAdmin(admin.ModelAdmin):
    list_display = ['firstName', 'lastName']

And the views is:

from django.shortcuts import render
from .models import Physiotherapist, HoursTemplate
from django.utils import timezone
from django.http import HttpResponse


def post_list(request):
    if request.method == 'POST':
        physiotherapist_id = request.POST.get('physiotherapist')


        print(f"ID fizjoterapeuty: {physiotherapist_id}") #working on response data capture


    physiotherapist = Physiotherapist.objects.order_by('-lastName')
    hours_template = HoursTemplate.objects.all()
    return render(request, 'fizjo/post_list.html', {'physiotherapist': physiotherapist, 'hours_template': hours_template})

When encountering an issue you don’t know how to handle, it’s frequently helpful to work on what you do know. If nothing else, doing that helps narrow down just how much remains unknown.

Do you have your models designed yet? If not, that’s the place to start. Figuring out how to create and handle your forms is going to be a lot easier once you know the data that they’re going to be used with.

So, what do your models look like so far?

1 Like