- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下错误:
django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as it is a non-editable field
我有很多模型可以有很多图像。所以我使用了django的GenericRelation。但是当我在 forms.py
中添加“图片”字段时。我收到错误
表单.py
class BuildingForm(ModelForm):
class Meta:
model = Building
fields = ['landlord', 'address', 'pictures']
模型.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'))
View .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())
表单.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>
跟踪
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
最佳答案
GenericForeignKeys 不适合与 ModelForms 一起使用。它们不可故意编辑。您也不能对它们使用filter() 或exclusion()。您可以引用文档以获取更多信息:
https://docs.djangoproject.com/en/2.1/ref/contrib/contenttypes/#generic-relations
关于python - 如何在 Django 表单中添加 GenericRelation 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910847/
我有一个事件对象,除了 Notes 之外,还有其他对象与 Event 有一般关系,并且没有事件字段。现在我希望能够编写一个查询来排除所有 Events,其中 Notes 的事件字段为 False。所以
在 Django 项目中,我有这样定义的模型: from django.db import models from django.contrib.contenttypes.models import
我想找到与我的角色相关且适合各种类别的属性。最终我想要这个输出: "Attributes": { "Physical": { "Level": 1,
我一定是真的误解了 Django 内容类型框架中的 GenericRelation field。 要创建一个最小的独立示例,我将使用教程中的投票示例应用程序。在 Choice 模型中添加一个通用外键字
我有 mixin 和模型: class Mixin(object): field = GenericRelation('ModelWithGR') class MyModel(Mixin, m
假设我有几个代表现实生活对象的模型:“人”、“椅子”、“房间” 我还有一个“Collection”模型,它代表了这些模型的一些记录集合。 每个模型可以是多个集合的成员——因此,我还创建了一个“Memb
我在将 GenericRelation 与 update_or_create 结合使用时遇到问题。我有以下型号: class LockCode(TimeStampedModel): conte
我想进行数据迁移,以便在数据库中添加用户阅读帖子。有这样的代码: def user_read_posts(apps, schema_editor): User = apps.get_model
我正在建立词汇表并有以下模型: class Word(Model): name = CharField(max_length=75) class EnNoun(Model): word = One
我有一个像这样的文章模型 from django.contrib.contenttypes.fields import GenericRelation from django.db import mo
我的 Django 应用程序中有这两个模型: class ItemOrigin(models.Model): content_type = models.ForeignKey(ContentT
我有以下错误: django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as
我正在使用django-import-export导出记录的模块。但是,我无法导出通用关系。我只想获取 GenericRelation 的所有详细信息。 在 Github 中找到了下面的代码片段,但它
在我的 Django 项目中,我有一个名为 Value 的模型,它具有这样的 GenericForeignKey: class Value(models.Model): content_typ
更新:关于此问题的公开标记:24272 到底是怎么回事? Django 有一个 GenericRelation类,它添加了一个“反向”通用关系以启用额外的API。 事实证明,我们可以将这个 rever
我有一些 Django 模型通用关系字段,我希望它们出现在 graphql 查询中。 Graphite 烯是否支持通用类型? class Attachment(models.Model): u
我想包含一个带有 GenericRelation 的模型DRF 中的反向引用 文档表明这应该很容易(就在上面:http://www.django-rest-framework.org/api-guid
我是一名优秀的程序员,十分优秀!