Show different verbose names for django admin models based on a condition

I have a Django model and I am trying to do something like this in my Django admin.

from django.contrib.auth.models import User

class Patient(models.Model):
    id = models.AutoField(primary_key=True)
    patient_ID = models.ForeignKey(PatientProfile)
    temp = models.CharField(max_length=10, blank=True, editable=True)
    pulse = models.CharField(max_length=10, blank=True, editable=True)

    class Meta:  
        if user.is_superuser:                                            
            verbose_name = "Doctor Consulting"
            verbose_name_plural = "Doctor Consulting"        

        else:                                                                    
            verbose_name = "Patients Details"
            verbose_name_plural = "Patients Details"  

But this is not working. I have already registered the model in my admin. I get an error like this;

if user.is_superuser: NameError user is not defined

Any help will be appreciated. Thanks

Upvotes: 1

Views: 1132

Answers (1)

After a week of research i found out about proxy models in Django and these proxy models can be given meta verbose names. Hence i went ahead to register different proxy models (for the same parent model) and then i gave them the different verbose names depending on the user group. Like so;

class Patient(models.Model):
    id = models.AutoField(primary_key=True)
    patient_ID = models.ForeignKey(PatientProfile)
    temp = models.CharField(max_length=10, blank=True, editable=True)
    pulse = models.CharField(max_length=10, blank=True, editable=True)

class Meta:                                              
        verbose_name = "Doctor Consulting"
        verbose_name_plural = "Doctor Consulting"        


class PatientProxy(Patient):
class Meta:
    proxy = True
    verbose_name = "Nurse Consulting"
    verbose_name_plural = "Nurse Consulting" 

Now in the django admin i can select which user can see which verbose name although it is the same model. i hope this will help somebody one day.

Upvotes: 2

Related Questions