filtering the value of a foreignkey based on another foreignkey in admin panel

Hi everyone
I want to filter values of a foreignkey (subbranch1) based on another foreignkey (mainbranch) in admin panel (specificly in adding a product to good table) how could i do that?

Thanks

here is my models

MainBranch

class MainBranch(models.Model):
    main_desc = models.CharField(verbose_name='توضیحات')

    def __str__(self):
        return self.main_desc

SubBranch1

class SubBranch1(models.Model):
    mainbranch = models.ForeignKey(
        MainBranch, on_delete=models.CASCADE,
        verbose_name='کد دسته اصلی'
    )
    sb1_desc = models.CharField(verbose_name='توضیحات')

    def __str__(self):
        return self.sb1_desc

Good model

class Good(models.Model):
    mainbranch = models.ForeignKey(
        MainBranch, on_delete=models.CASCADE,
        verbose_name='کد دسته اصلی', default=0
    )
    subbranch1 = models.ForeignKey(
        SubBranch1, on_delete=models.CASCADE,
        verbose_name='کد دسته فرعی 1', default=0
    )
    subbranch2 = models.ForeignKey(
        SubBranch2, on_delete=models.CASCADE,
        verbose_name='کد کالا', default=0
    )
    subbranch3 = models.ForeignKey(
        SubBranch3, on_delete=models.CASCADE,
          verbose_name='نام کالا', default=0, related_name='good_name')
    goodproperties = models.OneToOneField(
        GoodProperties, on_delete=models.CASCADE,
        verbose_name='مشخصات کالا', null=True, blank=True
    )
    goodspecifications = models.OneToOneField(
        GoodSpecification, on_delete=models.CASCADE,
        verbose_name='معرفی کالا', default=0, null=True, blank=True
    )    
    image = models.ImageField(upload_to='photos/%Y/%m/%d', null=True, blank=True, verbose_name='تصویر کالا')
    quantity = models.PositiveIntegerField(default=0, verbose_name='تعداد موجود')
    price = models.DecimalField(
        decimal_places=0, max_digits=12, default=0,
        verbose_name='قیمت کالا'
    )
    discount = models.SmallIntegerField(null=True, blank=True, verbose_name='میزان تخفیف')
    seller = models.ManyToManyField('Seller', verbose_name='کد فروشنده')

    @property  # getting all comments related to a specific good
    def get_comments(self):
        goods_comment = self.comment.all()
        return goods_comment

    def __str__(self):
        return self.subbranch3.sb3_desc

For clarification, are you saying that when someone is looking at the admin page for Good, and they make a selection for mainbranch, you want the admin page to dynamically update the available selections for subbranch1?

Yes that’s correct
i wrote a piece of code as below

class GoodAdmin(admin.ModelAdmin):
    form = GoodAdminForm
    
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "subbranch1":
            kwargs["queryset"] = SubBranch1.objects.filter(mainbranch=1)
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

it filters subbranch1 based on mainbranch
the problem is getting the value of mainbranch (in the code above i hardcode it as 1)
I want to grab the value of mainbranch dropdown then use it for filtering but don’t know how to do it

What you’re describing here would work for setting the initial value when the form is first rendered. However, this is not going to change what’s currently displayed on the page if the “mainbranch” field is changed but not submitted.

So please clarify - do you want the initial queryset for “subbranch1” set based upon what the current value is of “mainbranch”? Or are you looking to have the admin change the queryset for “subbranch1” dynamically, based upon a change made to “mainbranch”?

I want subbranch1 changes dynamically based on mainbranch

Since this is something happening within the browser, Django can’t directly help you here. You’d need some JavaScript code to catch the change event on mainbranch and then change subbranch1.

You would need to implement something like django-smart-selects for that.

1 Like

Thank you for your help