Doubt About the logged in user

Asked

Viewed 111 times

-1

I’ve been put on hold for lack of work opportunity in my region. Now I’ve been invited to participate in a project and I’ve come across a problem that for me is complicated.

I need to start a field with the group name and/or 'unit' this is the name of the field I created, to which the logged-in user belongs, I created a class to define and this 'unit' and I’m trying to rescue in my form, porblem aé que ta voltar vazio initially concei cirando a function now and finally a class. the code is now giving result that is a Quary/Property type in capsule or returns empty when I use the class, my code was like this:

I am using Python3 and Django3

py.models

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    unidade = models.CharField(max_length=100)

    def __str__(self):
        return self.unidade

Forms.py

class get_origem(Employee):

    def user_logado(slef):
        return slef.user.is_authenticated()

    def get_origem(self, user_logado):
        usuario = User.objects.get(username=user_logado)
        grupo_unidade = usuario.employee.unidade

        if grupo_unidade == user_logado:
            self.grupo_unidade

        else:
            self.grupo_unidade = 'NAO PERTENCE AO GRUPO'

            return self.grupo_unidade

        return self.grupo_unidade

class Cadastro_Unico_Add_Form(ModelForm):

    def __init__(self, *args, **kwargs):
        super(Cadastro_Unico_Add_Form, self).__init__(*args, **kwargs)
        self.fields['hospital_de_origem'].initial = get_origem

doubt is how to make the result return the logged user group or unit.

the other way I tried was this:

@property
def user_logado():
    return request.user.is_authenticated()

@user_logado.getter
def get_origem():
    usuario = User.objects.get(username=user_logado)
    grupo_unidade = usuario.employee.unidade

    if usuario == get_origem:
        grupo_unidade

    else:
        grupo_unidade = 'NAO PERTENCE AO GRUPO'

    return grupo_unidade

That returns it to me: <property object at 0x7f2c84ddd470> which I believe is the encapsulated result.

  • I forgot to mention, I did a lot of research I read in the Ango documentation, I understood that the @Property in capsule the class information, but I was not sure how to show this information.

1 answer

1


There is a certain difficulty in accessing instances of request within Forms, there are several tricks to this.

Use the Django-globals library, which allows you to access the user and request from anywhere.

Django-globals

When to start the form:

from django_globals import global

class Cadastro_Unico_Add_Form(ModelForm):

    def __init__(self, *args, **kwargs):
        super(Cadastro_Unico_Add_Form, self).__init__(*args, **kwargs)
        self.fields['hospital_de_origem'].initial = global.user.employee.unidade

Simple and clean solution

There are other ways, an interesting discussion: https://stackoverflow.com/questions/1057252/how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clean-met

  • Perfect! Thank you very much! worked at first.

  • 1

    @MARCELOPEREIRADASILVA, If this answer solved your problem and there was no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

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