Django , save_model , admin

okay the issue i am facing is that i am being able to add and save a user , no problem in that and once created , than i can easily assign therapist to that user again no problem , the problem arises when i try to assign the user with a therapist while creating the user , i am realy not getting that where am i making the mistake i mean , i have this feeling that i am assigning things to the user before even creating it but i cant find out which block of code is doing so or maybe just maybe the save_model is working in such a manner that that block code or block of codes need to be rearranged , kindly guide me

def save_model(self, request, obj, form, change):
    # If this is an existing instance and ending is set, prevent changing treatment_therapist
    if change and obj.client_current_status in UserJourneyStatus.ending_status() and 'treatment_therapist' in form.changed_data:
        return  # Don't save the change to treatment_therapist
    
    if 'treatment_therapist' in form.changed_data:
        # If this is the first time assigning a therapist
        if not obj.therapist_assigned_at and obj.treatment_therapist:
            obj.therapist_assigned_at = now()
        # If changing to a new therapist, update the assignment date
        elif obj.treatment_therapist:
            obj.therapist_assigned_at = now()
        
        reason_for_change = form.cleaned_data.get('reason_for_change', 'Updated via admin')
        TherapistHistory.objects.create(
            user=obj,
            therapist_name=obj.treatment_therapist.get_full_name() if obj.treatment_therapist else 'Unassigned',
            transfer_date=obj.updated,
            reason_for_change=reason_for_change,
        )
    
    if change:
        original = User.objects.get(pk=obj.pk)
        if original.client_current_status != obj.client_current_status:
            obj.status_changed_at = now()

            # Auto-set ending_datetime if status is in ending_status
            if obj.client_current_status in UserJourneyStatus.ending_status():
                obj.ending_datetime = now()
                if not obj.archived_at:
                    obj.archived_at = now()
                # if status is archived, preserves previous ending info and just mark archived_at
                if original.client_current_status in UserJourneyStatus.ending_status() and \
                   original.client_current_status != UserJourneyStatus.ARCHIVED:
                    obj.client_status_note = f"Archived after ending:{original.client_current_status}"
            else:
                obj.ending_datetime = None

            UserStatusHistory.objects.create(
                user=obj,
                journey_status=obj.client_current_status,
                journey_notes=obj.client_status_note or "No notes were added for this status change",
                changed_by=request.user,
            )
    
    super().save_model(request, obj, form, change)

The therapist should be assigned and object should be saved after.

obj.therapist_assigned =form.xyz

...

...

obj.save()

Currently, the python logic used in this (below) is incorrect, because it checks if there is no therapist assigned at a time (when creating a new user and trying to assign a therapist, this should result in true), then it checks if the newly created user has an assigned therapist, which will result in False, making it skip the whole first part of the if statement due to how the and operator works in python. It will then go into the elif statement below, and the logic inside it might err with a new user that has no therapist assigned yet. Instead, use this code (the CORRECT one, not the WRONG one):

*** If this helped, don't forget to click the checkbox and/or upvote!

# WRONG
if not obj.therapist_assigned_at and obj.treatment_therapist:
            obj.therapist_assigned_at = now()

# CORRECT
if not obj.therapist_assigned_at and not obj.treatment_therapist:
            obj.therapist_assigned_at = now()
Вернуться на верх