How to get the current user in the Django model?

1

Hello,

I am creating an application in django and I have a model with the NewDemand class in which I need when the user creates a demand, the face name is saved in a field. Here's my class:

class NewDemand(models.Model):

    name = models.CharField(max_length=150)
    date_created = models.DateTimeField(auto_now_add=True, blank=True)
    date_max = models.DateField(db_index=True)
    requester_user = models.ForeignKey(User)
    demand_desc = models.TextField()

    def __str__(self):
        return str(self.name)

I tried to put a models.ForeugnKey(User) but on the admin page a field appears with a menu that contains all the users. I want the user name to go directly to the database without having to appear on the admin page at the time of inputting the data.

    
asked by anonymous 12.06.2017 / 17:03

1 answer

1

You apply this logic to the view:

def new_demand(request):
    user = request.user.id
    demand = NewDemand(name, date_created, date_max, user, demand_desc)

So you get the user ID logged in and pass as a parameter to your demand.

    
12.06.2017 / 18:14