简体   繁体   中英

Django: How to Filter Model with "where" clause to display in list admin related objects

I have 2 models that depend on each other. One is the FeatureFilm model and the other is the CompanyInvolved model. In the FeatureFilm table I want to store movies and in the CompanyInvolved table I want to store the many companies that have contributed to a movie. The CompanyInvolved model is attached to the FeatureFilm table with a foreignkey. I want to display a column from the CompanyInvolved table in the list view of the FeatureFilm Admin Panel. I have already done that with my code. The only thing I didn't get yet is a filter that filters the CompanyInvolved table on the field company_role . I want to show only the values that have the company_role = "Produktion" . oh yes I want to show only the first production company, the other rows do not play a role in the list tables view. many greetings Can someone please help? many greetings

here is my depending code:

model.py

company_role = [
    ("Produktion", "Produktion"),
    ("Co-Produktion", "Co-Produktion"),
    ("Kinoverleih", "Kinoverleih"),
    ("Sender", "Sender"),
    ("Weltvertrieb", "Weltvertrieb"),
]

class ProjectBaseModel(models.Model):
    title = models.CharField("Titel", max_length=100, blank=False,
    unique=True)

    leading_postproduction_id = models.ForeignKey(
        Company,
        verbose_name="Federführende Postproduktion",
        on_delete=models.SET_NULL,
        blank=True,
        null=True,
    )



class FeatureFilm(ProjectBaseModel):
    class Meta:
        verbose_name = "Kinofilm"



class CompanyInvolved(models.Model):
    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
    )
    company_role = models.CharField(
       choices=company_role,
       max_length=15,
       blank=True,
       help_text="Produktion, Co-Produktion, Kinoverleiher, Sender,
       Weltvertrieb",
    )
    company_involved = models.ForeignKey(
        Company,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )

    def __str__(self):
        return "#" + str(self.pk)

views.py

class FeatureListView(LoginRequiredMixin, PermissionRequiredMixin, ListView):
    permission_required = "project.can_access_featurefilm_list"
    model = FeatureFilm
    template_name = "project/feature-list.html"

    def handle_no_permission(self):
       return redirect("access-denied") 

admin.py --- here is the code that is my problem****look to get_company_involved

@admin.register(FeatureFilm)
class FeatureFilmAdmin(admin.ModelAdmin):
    inlines = [
        SalesInfoSetInLine,
        ContentInfoSetInLine,
        CompanyInvolvedSetInLine,
    ]

    list_display = [
        "title",
        "get_company_involved",   
        "program_length_planned",
    ]

def get_company_involved(self, obj):
    productioncompany = (
        FeatureFilm.objects.filter(pk=obj.id)
        .values("companyinvolved__company_involved__name")
        .first()
    )
    return productioncompany["companyinvolved__company_involved__name"]

get_company_involved.short_description = "Produktion"

i tried more times and solves my question. here is the solution, in get_production_involved you can see the correct query

@admin.register(FeatureFilm)
class FeatureFilmAdmin(admin.ModelAdmin):
    inlines = [
        SalesInfoSetInLine,
        CompanyInvolvedSetInLine,
    ]

    list_display = [
        "title",
        "get_production_involved",
          ]

    def get_production_involved(self, obj):
        production_company = (
            FeatureFilm.objects.filter(
                pk=obj.id,
                companyinvolved__company_role="Produktion",
                companyinvolved__is_production_list=True,
            )
            .values_list("companyinvolved__company_involved__name")
            .first()
        )

        if production_company:
            return production_company
        else:
            production_company = (
                FeatureFilm.objects.filter(
                    pk=obj.id,
                    companyinvolved__company_role="Produktion",
                )
                .values_list("companyinvolved__company_involved__name")
                .first()
            )
            return production_company

    get_production_involved.short_description = "Produktion"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM