Django modelform没有返回id

时间:2015-07-08 15:27:04

标签: python django

我有一个简单的观点:

def admin_videos(request):
    if request.method=="POST":
        print(request.POST)
    else:
        a = Note.objects.get(pk=1)
        form = NoteForm(instance=a)
        return render(request, 'administration/index.html',{"form":form})

和一个模型:

class NoteForm(ModelForm):
    authors = ModelMultipleChoiceField(queryset=User.objects.all())
    class Meta:
        model = Note
        fields = '__all__'
        widgets = {
            'note': Textarea(attrs={'class': "form-control note", 'rows': 5}),
        }
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if 'instance' in kwargs:
            self.fields['note'].widget.attrs['data-id'] = kwargs['instance'].id

但是当我尝试更新现有实例时:

<form method="POST" action="">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit"/>
</form>

实例的id不在request.POST中 我很感激任何建议

0 个答案:

没有答案