我有一个这样的模型:
class Package(models.Model):
package_name = models.CharField(max_length=200)
package_price = models.IntegerField()
class UserPackage(models.Model):
user = models.ForeignKey(User)
package = models.ForeignKey(Package)
ModelForm是这样的:
class UserPackageForm(ModelForm):
class Meta:
model = UserPackage
exclude = ['user']
我想列出排除packages
的{{1}},这就是我这样做的原因:
Free
它也显示def __init__(self, *args, **kwargs):
super(UserPackageForm, self).__init__(*args, **kwargs)
self.fields['package'].queryset = Package.objects.exclude(package="Free")
包。怎么了?
答案 0 :(得分:1)
exclude参数错误,请尝试使用package_name而不是package。
from django.forms.models import BaseModelFormSet
class UserPackageForm(ModelForm):
def __init__(self, *args, **kwargs):
super(UserPackageForm, self).__init__(*args, **kwargs)
self.fields['package_name'].queryset = self.fields['package_name '].queryset.exclude(package_name="Free")
答案 1 :(得分:0)
试试这个
self.fields['package'].queryset = Package.objects.exclude(package__name="Free")