Validate csv file in Django

Asked

Viewed 123 times

0

I am trying to insert a function to validate the upload of a . csv, but when placing it the following error was presented:

ImportError: cannot import name 'PresencaProf' from partially initialized module 'parte1.models' (most likely due to a circular import)

I researched, but no solution was useful to me. I don’t know what this mistake is about, I just got a sense when I saw it running sequentially, but it didn’t fit, because as far as I could tell, everything was right.

Filing cabinet forms.py:

from django.forms import forms
from django import forms
from parte1.models import PresencaProf, PresencaAula


class UploadArquivo(forms.ModelForm):


    class Meta:
        model = PresencaProf
        fields = ('evento', 'data', 'upload_csv',)


        def __init__(self, *args, **kwargs):
            super(UploadArquivo, self).__init__(*args, **kwargs)
            for visible in self.visible_fields():
                visible.field.widget.attrs['class'] = 'form-control'

        # Funcao para validar extensao csv
        def valida_csv_extensao(value):
            if not value.fk_evento__nome_evento.endswith('.csv'):
                raise forms.ValidationError("Erro!\nApenas arquivos .csv !")

        def clean(self):
            data = super(UploadArquivo, self).clean()

All help is welcome!!!

Filing cabinet models.py:

from django.core.validators import FileExtensionValidator
from django.db import models
from chamadas import forms


class PresencaProf(models.Model):

    data = models.DateField()
    evento = models.ForeignKey(Eventos, on_delete=models.RESTRICT)
    # validators=[FileExtensionValidator(['csv'] -> valida um csv
    upload_csv = models.FileField(upload_to = 'csv', blank=True, null=True)


    class Meta:
        verbose_name = "Regristo de presença"
        verbose_name_plural = "Registros de presenças"


class PresencaAula(models.Model):

    # atributo que pega o ID de um evento, relacionamento 1:N
    fk_evento = models.ForeignKey(Eventos, on_delete=models.RESTRICT, null=True, blank=True)
    fk_professor = models.ForeignKey(Professor, on_delete=models.RESTRICT, null=True, blank=True)
    data_aula = models.DateField()

    class Meta:

        verbose_name = "Presença por aula"
        verbose_name_plural = "Presenças por aulas"
  • on Forms.py you have from parte1.models import PresencaProf, PresencaAula and in models.py from chamadas import forms. In other words, A imports B and B imports A. This results in import circular. I would say want you nay need to load the Forms from inside the models.

1 answer

1


in the imports of its py.models

from django.core.validators import FileExtensionValidator
from django.db import models
from chamadas import forms

removes the last line:

from django.core.validators import FileExtensionValidator
from django.db import models

These circular references are usually caused by imports, so always give a check...

  • Thank you very much, it worked. Now the mistakes are other kkkkk. Thank you, really!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.