Try to save multiple files using a one to many fields django

36 views
Skip to first unread message

jose angel encinas ramos

unread,
Feb 22, 2021, 12:04:49 AM2/22/21
to Django users
hello everyone, i working in my inventory django app, in this project i try to create a new area and in to each area has a posible to save multiples pdf.
so, when i try to save pdf in some new area have this error in terminal

'''
django.db.utils.IntegrityError: null value in column "microbusiness_id" violates not-null constraint
DETAIL:  Failing row contains (3, documents/Informe_de_TeleTrabajo.docx, 2021-02-22 04:25:32.352176+00, 2021-02-22 04:25:32.352206+00, null).

[22/Feb/2021 04:25:32] "POST /add_pdf HTTP/1.1" 500 18705

but its save it and does't show anything
'''
Model:
'''
class MicroBusiness(models.Model):
#models micro business
name = models.CharField(max_length=50)
img = models.ImageField(upload_to='areas_logo', null=True, blank=True)
comments = models.TextField(blank=True, null=True)

#actions
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)

class Meta:
verbose_name = 'MicroBusiness'
verbose_name_plural = 'MicroBusinesses'

def __str__(self):
return self.name

class DocumentsPdf(models.Model):
document = models.FileField(upload_to='documents', null=False, blank=False)
microbusiness = models.ForeignKey('MicroBusiness', on_delete=models.CASCADE, null=False, blank=False)

#actions
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)

class Meta:
verbose_name = 'DocumentPdf'
verbose_name_plural = 'DocumentPdfs'

def __str__(self):
return "%s" % self.document
'''
Form:
'''
class DocumentPdfForm(forms.ModelForm):
  #Documents pdf
    class Meta():
        model = DocumentsPdf
        fields = ('document',)
'''
Views:
'''
@permission_required('is_superuser')
@login_required
def AreasViews(request, id):
pdfdoc = DocumentsPdf.objects.all()
try:
data = MicroBusiness.objects.get(id = id)
except MicroBusiness.DoesNotExist:
raise Http404('Data does not exist')
context ={
'pdfdoc': pdfdoc,
'data':data,
}
return render(request,'inventory/organization/Areas.html',context)

@permission_required('is_superuser')
@login_required
def save_pdf(request,form,template_name):
#function save documents
data = dict()
if request.method == 'POST':
if form.is_valid():
""" import pdb
pdb.set_trace() """
form.save()
data['form_is_valid'] = True
pdfdoc = DocumentsPdf.objects.all()
data['areas_mb'] = render_to_string('inventory/organization/all_documents.html',{'pdfdoc':pdfdoc})
else:
data['form_is_valid'] = False
context = {
'form':form
}
data['html_form'] = render_to_string(template_name,context,request=request)
return JsonResponse(data)

@permission_required('is_superuser')
@login_required
def createpdf(request):
#this function add document
if request.method == 'POST':
form = DocumentPdfForm(request.POST or None, request.FILES)
else:
form = DocumentPdfForm()
return save_pdf(request,form,'inventory/organization/upload.html')


'''





Ryan Nowakowski

unread,
Feb 22, 2021, 10:10:47 PM2/22/21
to django...@googlegroups.com


On February 21, 2021 11:04:49 PM CST, jose angel encinas ramos <encinas...@gmail.com> wrote:
>hello everyone, i working in my inventory django app, in this project i
>try
>to create a new area and in to each area has a posible to save
>multiples
>pdf.
>so, when i try to save pdf in some new area have this error in terminal
>
>'''
>django.db.utils.IntegrityError: null value in column "microbusiness_id"
>
>violates not-null constraint
>DETAIL: Failing row contains (3,
>documents/Informe_de_TeleTrabajo.docx,
>2021-02-22 04:25:32.352176+00, 2021-02-22 04:25:32.352206+00, null).
>
>[22/Feb/2021 04:25:32] "POST /add_pdf HTTP/1.1" 500 18705
>
>but its save it and does't show anything

That error is telling you that you are trying to save a DocumentPdf without the required associated MicroBusiness. When you create the DocumentPdfForm, use the kwarg "instance" to initialize the form with a DocumentPdf with a non-null MicroBusiness.

>'''
Reply all
Reply to author
Forward
0 new messages