Python 如何在Django中的表单中添加GenericRelation字段

Python 如何在Django中的表单中添加GenericRelation字段,python,django,django-1.10,Python,Django,Django 1.10,我有以下错误: django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as it is a non-editable field 我有很多模型,可以有很多图像。所以我用了django的一般关系。但是当我在forms.py中添加“图片”字段时。我犯了一个错误 forms.py class BuildingForm(ModelForm): class Meta:

我有以下错误:

django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as it is a non-editable field
我有很多模型,可以有很多图像。所以我用了django的一般关系。但是当我在
forms.py
中添加“图片”字段时。我犯了一个错误

forms.py

class BuildingForm(ModelForm):
    class Meta:
        model = Building
        fields = ['landlord', 'address', 'pictures']
models.py

from stdimage.models import StdImageField

class Image(TimeStampedModel, models.Model):
    picture = StdImageField(upload_to='pictures/%Y/%m/%d',
                            verbose_name="pics", null = True, blank = True, variations={
        'large': (600, 400),
        'thumbnail': (250, 250, True),
        'medium': (300, 200),
    }, default='default.jpg')
    # Generic Foreign Key
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()


class Building(TimeStampedModel, models.Model):
    landlord = models.ForeignKey(
                settings.AUTH_USER_MODEL,
                related_name='building_manager')
    address = models.CharField(max_length=50, blank=False)
    pictures = GenericRelation(Image, null = True, blank = True,
        related_query_name='dwelling_picture', verbose_name=_('Screenshots'))
views.py

class BuildingCreateView(SuccessMessageMixin, CreateView):
    form_class = BuildingForm
    template_name = "parking/building/building_form.html"
    success_message = 'Successfully Added a Post entry'
    success_url = reverse_lazy('parkers:building_list')

    def form_valid(self, form):
        self.object = form.save(commit=True)
        #self.object.author = self.request.user
        return super(BuildingCreateView, self).form_valid(form)

parking_building_new = login_required(BuildingCreateView.as_view())
forms.html

<div class="container">
    <div class="row">
        <div class="col-lg-10 col-lg-1-offset">
            <form action="." method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Submit" />
            </form>

        </div>
    </div>
</div>

{%csrf_令牌%}
{{form.as_p}}
痕迹

文件“/home/laptopvm/Documents/Github/django\u project\u tutorial\u generirelation\u key/parking/url.py”,第4行,在
从parking.views导入用户\配置文件\视图
文件“/home/laptopvm/Documents/Github/django_project_tutorial_genericorrelation_key/parking/views/user_profile_views.py”,第26行
从parking.forms导入UserProfileForm
文件“/home/laptopvm/Documents/Github/django\u project\u tutorial\u generirelation\u key/parking/forms.py”,第7行,在
类BuildingForm(ModelForm):
文件“/home/laptopvm/anaconda3/lib/python3.6/site packages/django/forms/models.py”,第266行,在新的__
应用\u限制\u选项\u to=False,
文件“/home/laptopvm/anaconda3/lib/python3.6/site packages/django/forms/models.py”,第159行,在\u模型的字段中
f、 名称,型号。\u名称\u)
django.core.exceptions.FieldError:无法为建筑模型表单指定“图片”,因为它是不可编辑的字段

GenericForeignKey不适用于ModelForms。它们不是故意编辑的。您也不能对它们使用filter()或exclude()。您可以参考文档以了解更多信息:


GenericForeignKey不适用于ModelForms。它们不是故意编辑的。您也不能对它们使用filter()或exclude()。您可以参考文档以了解更多信息:

 File "/home/laptopvm/Documents/Github/django_project_tutorial_genericrelation_key/parking/urls.py", line 4, in <module>
    from parking.views import user_profile_views
  File "/home/laptopvm/Documents/Github/django_project_tutorial_genericrelation_key/parking/views/user_profile_views.py", line 26, in <module>
    from parking.forms  import UserProfileForm
  File "/home/laptopvm/Documents/Github/django_project_tutorial_genericrelation_key/parking/forms.py", line 7, in <module>
    class BuildingForm(ModelForm):
  File "/home/laptopvm/anaconda3/lib/python3.6/site-packages/django/forms/models.py", line 266, in __new__
    apply_limit_choices_to=False,
  File "/home/laptopvm/anaconda3/lib/python3.6/site-packages/django/forms/models.py", line 159, in fields_for_model
    f.name, model.__name__)
django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as it is a non-editable field