Issue
I use 'choice set' in my model but I can't show their labels in template
In model:
class Malpractice_Detail(models.Model):
DayTime_Choice={
('morning','صبح'),
('noon','ظهر'),
('night','شب'),
}
Damage_Choice={
('1','عدم آسیب'),
('2','آسیب به بیمار'),
('3','آسیب به پرسنل'),
('4','آسیب به تجهیزات پزشکی'),
}
malperactice_choice={
('1','خطاهای دارویی'),
('2','خطاهای احیاء قلبی-ریوی'),
('3','خطادرتزریقات وخونگیری'),
('4','خطادر ثبت اطلاعات وثبت پرونده'),
('5','خطاهای مراقبتی وبالینی'),
('6','اشتباهات پاراکلینیکی'),
('7','اشتباهات جراحی'),
('8','خطاهای تشخیصی پزشک'),
('9','اشتباهات تجهیزاتی'),
('10','اشتباهات مدیریتی')
}
hospital=models.ForeignKey('Accounting.hospital', verbose_name='بیمارستان',null=True, on_delete=models.DO_NOTHING)
name=models.CharField(max_length=50,verbose_name="نام شخصی که خطا از او سرزده",null=True, blank=True)
unit=models.ForeignKey('Accounting.unit', verbose_name='بخش',null=False, on_delete=models.DO_NOTHING)
time=models.CharField(max_length=10,choices=DayTime_Choice,null=False,blank=False,verbose_name='زمان وقوع')
type=models.CharField(max_length=50,choices=malperactice_choice,null=True,blank=True,verbose_name="نوع خطا")
date=models.DateField("تاریخ رخداد خطا", auto_now=False, auto_now_add=False)
role=models.ForeignKey('role', verbose_name='سمت خطاکار',null=False,blank=False, on_delete=models.DO_NOTHING)
damage=models.CharField(max_length=1,verbose_name='آسیب وارد شده',choices=Damage_Choice,default=1,null=False,blank=False)
st_happened=models.TextField(max_length=1000,verbose_name='شرح خطای رخ داده',null=False,blank=False)
decisions=models.TextField(max_length=1000,verbose_name='تصمیمات اتخاذ شده',null=True,blank=True)
cause_of_accident=models.TextField(max_length=1000,verbose_name='دلیل رخداد خطا',null=True,blank=True)
solution=models.TextField(max_length=1000,verbose_name='پیشنهاد و راهکار',null=True,blank=True)
user=models.ForeignKey(User, on_delete=models.DO_NOTHING)
reporting_datetime=models.DateTimeField(auto_now_add=True)
is_seen=models.BooleanField(verbose_name='دیده شده',default=False,null=True,blank=True)
has_followup=models.BooleanField(verbose_name='شرح پیگیری دارد',default=False,null=True,blank=True)
has_feedback=models.BooleanField(verbose_name='شرح بازخورد دارد',default=False,null=True,blank=True)
def __str__ (self):
return str(self.st_happened)
class Meta:
ordering=['-reporting_datetime']
In View:
def Index(request):
form=Malpractice_Detail.objects.select_related('Hospital','Unit','Role').filter(user=request.user).values('unit__name','date','role__Name','damage','st_happened','is_seen','has_followup','has_feedback')
return render(request,"malpractice/index.html",{'form':form})
In template: show damage code but not show damage label
{% for item in form %}
<td>{{item.date|to_jalali:'%Y/%m/%d'}}</td>
<td>{{item.damage}}</td>## This show number of choice
<td>{{item.get_damage_display}}</td> ##Not show label
<td>{{item.st_happened}}</td>
I reed many post about this and try solutions but don't work for me
please help me for solve this problem
Solution
Please don't use .values(…)
[django-antipaterns]: .values(…)
[Django-doc] provides dictionaries, and other native types, it is a primitive obsession antipattern [refactoring.guru] and will for example for ForeignKey
s return their primary key, not the corresponding model item.
Just let Django wrap these in Malpractice
objects, so that it ships with all the logic attached to it:
def Index(request):
form = Malpractice_Detail.objects.select_related(
'Hospital', 'Unit', 'Role'
).filter(user=request.user)
return render(request, 'malpractice/index.html', {'form': form})
You access the .name
of the .unit
then with:
{{ item.unit.name }}
Note: It might be better to use the
@login_required
decorator [Django-doc] as a decorator, and thus wrap the function in it like:from django.contrib.auth.decorators import login_required @login_required def Index(request): # …
Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be:
Hospital
instead of.hospital
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.