Django - 'Feed'对象不可调用

时间:2017-07-30 15:44:45

标签: python django django-forms

我正在尝试将此表单添加到我的项目中,我收到此错误:

'Feed'对象无法调用

当我只使用models.py时,它正在工作。这是我的代码:

forms.py

from django import forms
from .models import Feed


class FeedForm(forms.ModelForm):
    class Meta:
        model = Feed()
        fields = [
            'post'
        ]

models.py

from django.db import models


class Feed(models.Model):
    user = models.ForeignKey('auth.User')
    date = models.DateTimeField(auto_now_add=True)
    post = models.TextField(max_length=255)

    def __str__(self):
        return self.post

views.py

def post_new(request):
    if request.method == "POST":
        form = FeedForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.user = request.user
            post.date = timezone.now()
            post.save()
    else:
        form = FeedForm()
    return render(request, 'feeds/feed_edit.html', {'form': form})

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

模型应该只是模型的类名而不是模型的实例。

所以你的forms.py应该是这样的

from django import forms
from .models import Feed


class FeedForm(forms.ModelForm):
    class Meta:
        model = Feed
        fields = [
           'post'
         ]

有关详情,请访问以下链接:https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/