Tôi chỉ thấy rằng bạn có thể thay thế queryset với queryset khác, hoặc thậm chí loại bỏ các queryset và thay thế bằng một danh sách lựa chọn . Tôi làm điều này trong change_view.
Trong ví dụ này, tôi để cho phụ huynh thiết lập giá trị trả về, sau đó lấy các lĩnh vực cụ thể từ nó và thiết lập .choices:
def change_view(self, request, object_id, form_url='', extra_context=None):
#get the return value which includes the form
ret = super().change_view(request, object_id, form_url, extra_context=extra_context)
# let's populate some stuff
form = ret.context_data['adminform'].form
#replace queryset with choices so that we can specify the "n/a" option
form.fields['blurb_location'].choices = [(None, 'Subscriber\'s Location')] + list(models.Location.objects.filter(is_corporate=False).values_list('id', 'name').order_by('name'))
form.fields['blurb_location'].queryset = None
return ret
Nguồn
2014-03-30 03:21:25
1 điều nữa: tại sao biểu tượng PLUS không được hiển thị bên cạnh đầu vào đã chọn của tôi? Các trường không ghi đè có trường - trường tôi đã thay đổi - KHÔNG phải – robos85