Filter combobox according to the list of projects created by each user logged in to Django

Asked

Viewed 74 times

0

I’m new to Jango and I have a lot to learn. I’m developing a system in which the logged-in user registers their requirement, but each requirement is tied to a project that the same user created. As it is a multi-tenant system, then multiple users will use the system and each registers its own project and requirement.

I’m getting the following message

AttributeError at /requisitos/cadastrarRequisito/
'User' object has no attribute 'projeto'
Request Method: GET
Request URL:    http://127.0.0.1:8000/requisitos/cadastrarRequisito/
Django Version: 3.1.2
Exception Type: AttributeError
Exception Value:    
'User' object has no attribute 'projeto'

Follow my model

class Requisito (models.Model):
    nomeRequisito = models.CharField(max_length=30, verbose_name=('Nome do Requisito'))
    responsavel = models.CharField(max_length=30, verbose_name=('Responsável pelo Desenvolvimento'))
    código = models.CharField(max_length=20, verbose_name=('Código do Requisito'))
    projeto = models.ForeignKey(Projeto, on_delete=models.PROTECT)
    prioridade = models.CharField(max_length=10, verbose_name=('Prioridade do Requisito'))
    risco = models.CharField(max_length=10, verbose_name=('Risco do Requisito'))
    motivo = models.CharField(max_length=20, verbose_name=('Motivo do Requisito'))
    status = models.CharField(max_length=20, verbose_name=('Status do Requisito'))
    requisitosImpactados = models.CharField(max_length=100, verbose_name=('Requisitos Impactados'))
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    estoriaUsuario = HTMLField()
    regrasNegocio = HTMLField()

    def __str__(self):
        return self.nomeRequisito

Forms py.

class CadastrarRequisitos(ModelForm):
    def __init__(self, user, *args, **kwargs):
        super(CadastrarRequisitos, self).__init__(*args, **kwargs)
        self.fields['projeto'].queryset = Projeto.objects.filter(
             nomeProjeto=user.projeto.nomeProjeto)

    class Meta:
        model = Requisito
        fields = ['nomeRequisito',
                  'responsavel',
                  'código',
                  'projeto',
                  'prioridade',
                  'status',
                  'risco',
                  'motivo',
                  'requisitosImpactados',
                  'user',
                  'estoriaUsuario',
                  'regrasNegocio']

view

class RequisitoCreate(CreateView):
    model = Requisito
    form_class = CadastrarRequisitos
    
    def get_form_kwargs(self):
        kwargs = super(RequisitoCreate, self).get_form_kwargs()
        kwargs.update({'user': self.request.user})
        return kwargs

Model design

class Projeto(models.Model):
    nomeProjeto = models.CharField(max_length=20, verbose_name=('Nome do Projeto'))
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    descricao = HTMLField()

How to make each requirement relate to a project that the respective logged-in user has registered and does not show this error message? Thank you very much, from now on.

No answers

Browser other questions tagged

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