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