假设我们有这个型号:
class Event(models.Model):
name = models.CharField('Nome do evento', max_length=50)
code = models.CharField('Código de entrada', max_length=10)
artists_list = models.ForeignKey(ListGroup, on_delete=None,
related_name='lists_names', null=True)
和此视图
class HomeView(LoginRequiredMixin, TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['form'] = CreateEventForm(self.request.POST or None)
context['defaultTitle'] = 'Novo Evento'
context['formTitle'] = 'Criar'
return context
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
form = context['form']
print(form)
if form.is_valid():
form.save()
return self.render_to_response(context)
和本表格
class CreateEventForm(forms.ModelForm):
class Meta:
model = Event
fields = ('name', 'code', 'artists_list',)
一切都很好,但我希望能够选择我的外键将检索的多个条目。所以我想将每个条目渲染为复选框而不是选择。我怎样才能做到这一点?我已经搜索了很多,只发现了一般的charfields,没有关于Foreing Key的内容 Here's how its rendering
答案 0 :(得分:4)
外键不能指向多个条目。如果你想要,你应该使用ManyToManyField。