Django: want to use in admin list_display a property function from my model
I have a model (FeatureFilm) in which I use a "property" function to retrieve a specific person (SUPERVISOR) from another table (StaffInternal) that points to my FeatureFilm table with a ForeignKey.
In the template I can call this property and the SUPERVISOR is also shown to me in a list view.
Now I want to populate the list view in the admin panel also with this SUPERVISOR column. Here I now do not know how to call the property function.
here are my two Model:
class StaffIntern(models.Model):
HEAD_OF_POSTPRODUCTION = "HP"
HEAD_OF_SALES = "HS"
SUPERVISOR = "SV"
STAFF_INTERN = [
(HEAD_OF_POSTPRODUCTION, "Head of Postproduction"),
(HEAD_OF_SALES, "Head of Sales"),
(SUPERVISOR, "Postproduction Supervisor"),
]
feature_id = models.ForeignKey(
FeatureFilm,
on_delete=models.CASCADE,
null=True,
blank=True,
)
tv_movie_id = models.ForeignKey(
TvMovie, on_delete=models.CASCADE, null=True, blank=True
)
staff_role = models.CharField(
choices=STAFF_INTERN,
max_length=3,
blank=True,
help_text="Head of Postproduction, Head of Sales, Postproduction Supervisor, Technical Director, Colorist, "
"Re-Recording Mixer, Online Editing, VFX Editing, DCP Mastering ",
verbose_name="Stab Rolle",
)
staff_involved = models.ForeignKey(
CustomUser,
on_delete=models.SET_NULL,
null=True,
blank=True,
verbose_name="Beteiligte Person",
)
class FeatureFilm(ProjectBaseModel):
class Meta:
verbose_name = "Kinofilm"
verbose_name_plural = "Kinofilme"
ordering = ["title"]
@property
def production_companies(self):
return [
company
for company in self.companyinvolved_set.all()
if company.company_role == company.PRODUCTION
and company.is_production_list # PR = Produktion
]
@property
def internal_supervisor(self):
return [
staff
for staff in self.staffintern_set.all()
if staff.staff_role == staff.SUPERVISOR
]
in the template i access the property like this:
<tbody>
{% for project in object_list %}
<tr>
<td><a href="{% url 'feature-detail-date' project.pk %}">{{ project.title }}</a></td>
<td>
{% for production in project.production_companies %}{{production.company_involved.name}}{% endfor %}
</td>
<td>{% if project.program_length_planned %}
{{ project.program_length_planned }}
{% endif %}
</td>
<td>{{ project.global_shooting_resolution }}</td>
<td>{{ project.global_resolution_theatrical }}</td>
<td>{% if project.hdr == 1%}
ja
{% else %}
nein
{% endif %}
</td>
<td>{% for supervisor in project.internal_supervisor %}{{supervisor.staff_involved.username}}{% endfor %}</td>
<td>{% if project.phase %}
{{ project.get_phase_display}}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
this all works fine too, now i just need to somehow get this into the admin as well. I have not managed that so far. Here is my code that goes into an error:
@admin.register(FeatureFilm)
class FeatureFilmAdmin(admin.ModelAdmin):
inlines = [
SalesInfoSetInLine,
ContentInfoSetInLine,
]
list_display = [
"title",
"get_production_involved",
"get_shootingtime",
"program_length_planned",
"global_shooting_resolution",
"global_resolution_theatrical",
"hdr",
"get_supervisor",
]
def get_supervisor(self, obj):
supervisor = FeatureFilm.objects.filter(pk=obj.id).values(
"internal_supervisor__staff_involved__username"
)
return supervisor
get_supervisor.short_description = "Projektbetreuer"