Sunday, February 21, 2021

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

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')


'''





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8f95a86-b77b-480f-bbe4-ba7f76582731n%40googlegroups.com.

No comments:

Post a Comment