Python Django表单-基于外键值筛选列表
因此,基本上我的模型中有以下内容Python Django表单-基于外键值筛选列表,python,django,django-forms,Python,Django,Django Forms,因此,基本上我的模型中有以下内容 class Company (models.Model): title = models.CharField(max_length=100) short = models.CharField(max_length=50,default='NA') class AccountingGroups(models.Model): title = models.CharField(max_length=50, unique=True)
class Company (models.Model):
title = models.CharField(max_length=100)
short = models.CharField(max_length=50,default='NA')
class AccountingGroups(models.Model):
title = models.CharField(max_length=50, unique=True)
description= models.CharField(max_length=150,blank=True)
section = models.ForeignKey(Section, on_delete=models.PROTECT)
class Transaction (models.Model):
description = models.CharField(max_length=100)
date_created= models.DateField(auto_now_add=True)
posting_date = models.DateField()
user=models.ForeignKey(User, on_delete=models.PROTECT)
company = models.ForeignKey(Company, on_delete=models.PROTECT)
account_group = models.ForeignKey(AccountingGroups, on_delete=models.PROTECT)
income_amt = models.DecimalField(max_digits=6, decimal_places=2,default=0)
expenditure_amt = models.DecimalField(max_digits=6, decimal_places=2,default=0)
现在我在浏览器上显示交易表单,以便登录特定公司的所有收入和支出。下面是forms.py文件
class TransactionForm(ModelForm):
#posting_date = DateField(input_formats=['%d/%m/%Y'])
posting_date = DateField(widget=DatePickerInput(format='%d/%m/%Y').start_of('event active days'),
input_formats=('%d/%m/%Y',),
required=False)
class Meta:
model = Transaction
fields = ['description','posting_date','account_group','income_amt','expenditure_amt']
现在网站的结构是,我数据库中的每家公司都有一个不同的url。当我转到每个url时,我可以查看/编辑或创建该特定公司的新交易/现有交易。现在我要问的是,我是否可以限制表单,使其仅显示该特定公司的会计组,而不是显示所有会计组,无论我试图在哪个公司创建交易 如果要显示公司的会计组(而不是交易),则需要一个外键来过滤。除非FK节链接到公司,否则AccountingGroups表中当前没有任何内容。然后,您只需查询该外键关系
AccountingGroups.objects.filter(section__company_id=pk)
不管你怎么做,你都需要通过你的请求输入公司id