How to display on Django page based on user's condition?

11 views
Skip to first unread message

Pema Galey

unread,
Nov 21, 2019, 2:59:04 PM11/21/19
to Django users
Hi All,
I am Employee model directly link with User model and Employee holds User ID and division from Division model.
Every employee holds single Division.

class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
division = models.ForeignKey(
Division,


)
Now, I want to do following things:
1) Get current username /ID
2) Get Employee division ID/name
3) Display the other data based on Employee's division. [That particular Employee should be accessible to their own Division data only]

Someone help me to handle this one please.

Thank you in advance.

James Dunn

unread,
Nov 21, 2019, 6:07:02 PM11/21/19
to django...@googlegroups.com
In your view you can do:

user = request.user

Then you can get Employee with:

employee = Employee.objects.get(user=user)
Then just get the attributes from the models that you need:
eg:

employee.division


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a536c1fb-6852-48f9-8476-463c696926a2%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages