فایل ادمین پروژه

import jdatetime
from django.contrib import admin
from .forms import PatientsAppointmentForm
from .models import PatientsAppointment, PrescribedMedicationDetail
from app_patients.models import PatientsInfo
from app_doctors.models import DoctorsInfo

class PrescribedMedicationDetailInline(admin.TabularInline):
    model = PrescribedMedicationDetail
    extra = 1

@admin.register(PatientsAppointment)
class PatientsAppointmentAdmin(admin.ModelAdmin):
    form = PatientsAppointmentForm
    change_form_template = 'admin/app_healthFile/PatientsAppointment/change_form.html'
    
    list_display = ('get_patient_info', 'get_doctor_info', 'get_jalali_appointment_date')
    readonly_fields = ('appointment_date',)
    inlines = [PrescribedMedicationDetailInline]
    fields = (
        'patient',
        'doctor',
        'appointment_date',
        'symptoms_existing', 
        'symptoms',  
        'doctors_diagnosis_existing', 
        'doctors_diagnosis',  
        'medical_tests_existing',  
        'medical_tests', 
        'medical_imaging_existing', 
        'medical_imaging',  
    )

    def get_patient_info(self, obj):
        try:
            username = obj.patient.split('(')[-1].strip(')')
            patient_info = PatientsInfo.objects.get(sick__username=username)
            full_name = f"{patient_info.sick.first_name} {patient_info.sick.last_name}"
            return f"{full_name} (کد ملی: {patient_info.sick.username})"
        except PatientsInfo.DoesNotExist:
            return "اطلاعات بیمار یافت نشد"
    get_patient_info.short_description = 'بیمار - نام و کد ملی'

    def get_doctor_info(self, obj):
        try:
            medical_id = obj.doctor.split('(')[-1].strip(')')
            doctor_info = DoctorsInfo.objects.get(medical_id=medical_id)
            full_name = f"{doctor_info.user.first_name} {doctor_info.user.last_name}"
            return f"{full_name} (شماره نظام پزشکی: {doctor_info.medical_id})"
        except DoctorsInfo.DoesNotExist:
            return "پزشک: اطلاعات یافت نشد یا کد اشتباه است"
    get_doctor_info.short_description = 'پزشک - نام و شماره نظام پزشکی'

    def get_jalali_appointment_date(self, obj):
        # استفاده از jdatetime برای تبدیل تاریخ به شمسی
        jalali_date = jdatetime.datetime.fromgregorian(datetime=obj.appointment_date)
        return jalali_date.strftime('%Y/%m/%d')
    get_jalali_appointment_date.short_description = 'تاریخ مراجعه'

    def get_extra_context(self, request):
        return {
            'patients_list': [
                f"{patient.sick.first_name} {patient.sick.last_name} ({patient.sick.username})"
                for patient in PatientsInfo.objects.all()
            ],
            'doctors_list': [
                f"{doctor.user.first_name} {doctor.user.last_name} ({doctor.medical_id})"
                for doctor in DoctorsInfo.objects.all()
            ],
        }

    def add_view(self, request, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context.update(self.get_extra_context(request))
        return super().add_view(request, form_url, extra_context=extra_context)

    def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        extra_context.update(self.get_extra_context(request))
        return super().change_view(request, object_id, form_url, extra_context=extra_context)
    

فایل فورم

from django import forms
from django_select2.forms import Select2MultipleWidget
from .models import PatientsAppointment
from app_patients.models import PatientsInfo, PatientSymptoms, DiseaseInfo
from app_laboratory.models import MedicalTestTypes
from app_doctors.models import DoctorsInfo, Specialty
from app_medicalImaging.models import PhotographyTypes

class PatientsAppointmentForm(forms.ModelForm):
    patient = forms.CharField(
        label='بیمار',
        widget=forms.TextInput(attrs={'list': 'patients-list'}),
    )

    doctor = forms.CharField(
        label='پزشک معالج',
        widget=forms.TextInput(attrs={'list': 'doctors-list'}),
    )

    symptoms_existing = forms.MultipleChoiceField(
        label='انتخاب علائم',
        choices=[(symptom.name, symptom.name) for symptom in PatientSymptoms.objects.all()],
        required=False,
        widget=Select2MultipleWidget
    )

    symptoms = forms.CharField(
        label='علائم',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': 'وارد کردن علائم جدید'}),
    )

    doctors_diagnosis_existing = forms.MultipleChoiceField(
        label='انتخاب تشخیص‌ها',
        choices=[(diagnosis.name, diagnosis.name) for diagnosis in DiseaseInfo.objects.all()],
        required=False,
        widget=Select2MultipleWidget
    )

    doctors_diagnosis = forms.CharField(
        label='تشخیص‌ها',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': 'وارد کردن تشخیص‌های جدید'}),
    )

    medical_tests_existing = forms.MultipleChoiceField(
        label='انتخاب آزمایشات',
        choices=[(test.name, test.name) for test in MedicalTestTypes.objects.all()],
        required=False,
        widget=Select2MultipleWidget
    )

    medical_tests = forms.CharField(
        label='آزمایشات',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': 'وارد کردن آزمایشات جدید'}),
    )

    medical_imaging_existing = forms.MultipleChoiceField(
        label='انتخاب تصویربرداری‌ها',
        choices=[(imaging.name, imaging.name) for imaging in PhotographyTypes.objects.all()],
        required=False,
        widget=Select2MultipleWidget
    )

    medical_imaging = forms.CharField(
        label='تصویربرداری‌ها',
        required=False,
        widget=forms.TextInput(attrs={'placeholder': 'وارد کردن تصویربرداری‌های جدید'}),
    )

    class Meta:
        model = PatientsAppointment
        fields = '__all__'

    class Media:
        css = {
            'all': ('app_doctors/css/custom_select2.css',)
        }

    def clean_fields(self):
        cleaned_data = super().clean()
        # Add your custom cleaning logic if needed
        return cleaned_data



مشکلم اینه از پنل ادمین نمیتونم انتخاب علائم و انتخاب تشخیص‌ها و انتخاب آزمایشات و انتخاب تصویربرداری‌ها رو ذخیره کنم