猿问

foreginkey的过滤查询集

员工有两种类型的销售和普通员工,在销售表格中我只想显示销售员工


慕勒3428872
浏览 110回答 2
2回答

回首忆惘然

def __init__(self, user, *args, **kwargs):    super(BranchesMasterForm, self).__init__(*args, **kwargs)    self.fields['CompanyName'].queryset = BranchesMaster.objects.filter(user=user)

慕田峪7331174

你必须在 models.py 中创建两个类class Sales(models.model):    user = models.ForiegnKey(User , on_delete= models.CASCADE)    { add more attributes if u want } class Employee(models.model):    user = models.ForiegnKey(User , on_delete= models.CASCADE)    { add more attributes if u want }之后创建一个名为 forms.py 的新文件   class SalesForm(forms.Modelform):       class Meta:       model = Sales       fields = [ 'u can insert the fields which u want to show in ur form  ' ]同样,您为您的员工制作表单类NOTE:--u have to import ur model in your form.py moduleand do import--- from django.contrib.auth.models import User in ur models.py file  
随时随地看视频慕课网APP

相关分类

Python
我要回答