python - Use logged in username as field value in django -


How do I use the user's username as the default (and editable) value in the Django field?

For example, let's say that I'm trying to log in to the item purchased by a team of people I am using contrib.admin because it's my I fulfill the most needs, I define the line item purchased like this:

Line Line (model.Model): name = models.CharField (max_length = 40) vendor = model .ForeignKey (Seller) Buyer = & lt; Whoever logs this purchase & gt; Date_ordered = models.DateField ('date ordered', tap = true) date_delivered = models.DateField ('date distributed', zero = true) value = model. Decimal (Null = true = maximum, decimal = 9, decimal = 2) def __unicode__ (self): Returns self name

I have this code & amp; Something wrong with the name of the logged-in user?

PS I think there is another way in accordance with this question: where continuity of view-> database should I get the name of the user and store it?

You use the user model as a buyer in the line model as a buyer in the form of a foreign key can do. Whenever you are saving data in the line item model, in that view, you can get request.user as a logged in user and store it. As the buyer of that line item object

Create a lineitum object (in the post method) in the lineItem model class

  buyer = models.ForeignKey (user)  

To see / update the class / function, you can

  a_lineitem_object.buyer = request.user a_lineitem_object.save ()  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -