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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

jose angel encinas ramos
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8f95a86-b77b-480f-bbe4-ba7f76582731n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

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

Ryan Nowakowski


On February 21, 2021 11:04:49 PM CST, jose angel encinas ramos <[hidden email]> 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.

>'''
>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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02F09D34-5F12-4227-BB50-7B27DD577752%40fattuba.com.