模型
class Reservation(models.Model):
company = models.ForeignKey(GuestContact, on_delete=models.PROTECT)
class GuestContact(models.Model):
company = models.CharField(max_lenght=30)
last_name = models.CharField(max_lenght=30)
first_name = models.CharField(max_lenght=30)
contact_genere_id = models.ForeignKey(ContactGenere, on_delete=models.PROTECT) # 1 = company 2 = guest
形式
class ReservationForm(ModelForm):
class Meta:
model = Reservation
fields = '__all__'
widgets = {
'company': autocomplete.ModelSelect2()
}
视图
class GuestContactAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = GuestContact.objects.filter(contact_genere_id=1)
return qs
这个QuerySet过滤器在python shell中工作,在我的视图中这不起作用我看到所有联系人不仅是公司联系人。你能救我吗?
答案 0 :(得分:0)
我认为您需要一个网址供您查看: 像这样的东西:
from .views import GuestContactAutocomplete
urlpatterns = [
url(
r'^guest-contact-autocomplete/$',
GuestContactAutocomplete.as_view(),
name='guest-contact-autocomplete'
),
]
然后在表单中,将此url名称设置为表单字段小部件,如下所示:
class ReservationForm(ModelForm):
class Meta:
model = Reservation
fields = '__all__'
widgets = {
'company': autocomplete.ModelSelect2(url='guest-contact-autocomplete')
}