[Fixed]-How can I use __init__() in python django

1👍

✅

Not sure why you have PatientId as a class, but with that given class you can use it like this, since generate_patient_id_number() is a static method

id = PatientId.generate_patient_id_number()
# id is e.g. 'HSCM-E9E10C'

You don’t need a class for this

In Python you don’t need to make everything into a class, so if you had a module called id_number you can simply put that function there (and then you can use it from anywhere, a view, a class, a Django model, etc)

# id_number.py

 def generate_patient_id_number():
        prefix = 'HSCM'
        generated_id = str(uuid.uuid4().hex[:6].upper())
        return  '%s-%s' % (prefix, generated_id)

and use it like this

from utilities import id_number

def show_id(request):
    id = id_number.generate_patient_id_number()
    print(id)
    return render(request, 'index.html', {})

Leave a comment