ich habe gerade eben schonmal einen Post geschrieben, allerdings sieht es so aus als wenn er nicht gespeichert wurde Daher nochmal von vorne
Ich versuche zu einem bestehenden Model ein neues Model zu verknüpfen, um mehrere Dateien gleichzeitig hochzuladen.
urls
Code: Alles auswählen
urlpatterns = [
url(r'^$', views.MemoriesIndexListView.as_view(), name='index'),
url(r'^add-attachment/(?P<question_id>\d+)/$', views.UploadView.as_view(), name='attachment'),
### some more urls ###
]
Code: Alles auswählen
class Question(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='remember_user')
### some more fields ###
class Attachment(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
file = models.FileField(upload_to='attachments')
Code: Alles auswählen
class UploadView(FormView):
template_name = 'attachment_form.html'
form_class = UploadForm
def form_valid(self, form):
for each in form.cleaned_data['attachments']:
Attachment.objects.create(file=each)
return super(UploadView, self).form_valid(form)
def get_success_url(self):
return reverse("index")
Code: Alles auswählen
class UploadForm(forms.Form):
attachments = MultiFileField(min_num=1, max_num=10, max_file_size=1024*1024*5)
default=1 macht ja keinen SinnYou are trying to change the nullable field 'question' on attachment to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
Wie müsste ich das richtig verknüpfen?