Mac Fly
Mac Fly

Reputation: 21

Django - get model in navbar

I have a class in models.py :

class Customer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=200, null=True, blank=True)
    email = models.CharField(max_length=200, null=True, blank=True)
    device = models.CharField(max_length=200, null=True, blank=True)

Is there a way to access this model in navbar without creating entry in "views.py"? I would like to access similarly to {{ request.user.id }}.

Upvotes: 2

Views: 257

Answers (1)

iklinac
iklinac

Reputation: 15738

Customer is related to User using One-to-one relationship, you can get it through User object

request.user.customer

EDIT (after determining Customer and User are not really related):

You can write your own context processor which will return Customer object

Upvotes: 2

Related Questions