我有 2 个模型
class A(models.Model):
id=models.AutoField(primary_key=True)
name=models.CharField(max_length=200)
class B(models.Model):
id=models.AutoField(primary_key=True)
user=models.ForeignKey(A)
name=models.CharField(max_length=200)
#forms.py
class BForm(ModelForm):
class Meta:
model=B
fields=('name','user')
def __init__(self,user_name,*args,**kwargs):
super(BForm,self).__init__(*args,**kwargs)
if user_name:
self.field['user']=forms.ModelChoiceField(queryset=A.objects.filter(name__icontains=user_name)
#views.py
def myview(request,user_name):
formset=formset_factory(BForm(user_name=user_name),extra=10)
我收到以下错误
'BForm' 对象没有属性 ' name '
实际上我只希望在我的表单集中显示 A 类的选择性值