我遇到以下错误:
XMLDocument
我有很多型号,可以有很多图像。因此,我使用了django的GenericRelation。但是,一旦我在let xmlString = """
<data>
<button/>
<tag>
<button/>
</tag>
</data>
"""
let document = try XMLDocument(xmlString: xmlString, options: [.documentTidyXML])
for node in try document.nodes(forXPath: "//button") {
guard let buttonElement = node as? XMLElement else { continue }
buttonElement.setAttributesWith([
"customClass": "AmazingButton",
"customModule": "IBComponents",
"customModuleProvider": "target",
])
}
let newXMLString = document.xmlString(options: [.nodePrettyPrint])
print(newXMLString)
中添加了“ 图片”字段。我收到错误消息
forms.py
django.core.exceptions.FieldError: 'pictures' cannot be specified for Building model form as it is a non-editable field
models.py
forms.py
views.py
class BuildingForm(ModelForm):
class Meta:
model = Building
fields = ['landlord', 'address', 'pictures']
forms.html
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'))
跟踪
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())
答案 0 :(得分:3)
GenericForeignKeys不能与ModelForms一起使用。它们不是故意编辑的。您也不能对它们使用filter()或exclude()。您可以参考文档以获取更多信息:
https://docs.djangoproject.com/en/2.1/ref/contrib/contenttypes/#generic-relations