[PYTHON] How to get a logged-in user with Django's forms.py

Thing you want to do

In the FormsClass of forms.py, using the currently logged in user information I want to do something.

forms.py



class Form(forms.ModelForm):
    def method(self):
        model = Model
        model.objects.filter(user=[Logged in user]) #I want to use a logged-in user

solution

When creating a Form instance with views.py, pass the logged-in user information as an argument.

views.py



class View(View):
...
    form = Form(user=request.user, data=request.POST)
...

forms.py


...
class Form(forms.ModelForm):
    def __init__(self, user, *args, **kwargs):
        self.current_user = user
        super().__init__(*args, **kwargs)

    def method(self):
        model = Model
        model.objects.filter(user=self.current_user)
...

When to use

Used when validating with form.

reference

https://stackoverflow.com/questions/5119994/get-current-user-in-django-form

Recommended Posts

How to get a logged-in user with Django's forms.py
How to get started with Scrapy
How to get started with Python
How to get started with Django
How to get parent id with sqlalchemy
How to add a package with PyCharm
How to use ManyToManyField with Django's Admin
Here's a brief summary of how to get started with Django
How to get a stacktrace in python
[Python] How to get a value with a key other than value with Enum
How to get started with laravel (Linux)
How to read a CSV file with Python 2/3
How to send a message to LINE with curl
How to draw a 2-axis graph with pyplot
How to develop a cart app with Django
How to make a dictionary with a hierarchical structure.
How to create a multi-platform app with kivy
A layman wants to get started with Python
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to specify a schema in Django's database settings
[Python] How to draw a line graph with Matplotlib
How to create a submenu with the [Blender] plugin
How to convert a class object to a dictionary with SQLAlchemy
How to deal with Django's Template Does Not Exist
How to make a shooting game with toio (Part 1)
How to get more than 1000 data with SQLAlchemy + MySQLdb
How to get mouse wheel verdict with Python curses
[Python] How to create a 2D histogram with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
How to use fixture in Django to populate sample data associated with a user model
How to call a function
How to update with SQLAlchemy?
How to cast with Theano
How to hack a terminal
How to Alter with SQLAlchemy?
How to use Django's GeoIp2
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to deploy a web app made with Flask to Heroku
How to make a Cisco Webex Teams BOT with Flask
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
How to install NPI + send a message to line with python
How to kill a process instantly with Python's Process Pool Executor
How to output a document in pdf format with Sphinx
How to create a flow mesh around a cylinder with snappyHexMesh
How to print characters as a table with Python's print function
How to set the html class attribute in Django's forms.py
How to make a simple Flappy Bird game with pygame
I tried "How to get a method decorated in Python"
I tried to get started with Hy ・ Define a class
How to display a list of installable versions with pyenv
How to extract other than a specific index with Numpy
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
A story about how to deal with the CORS problem
How to build a python2.7 series development environment with Vagrant
How to get into the python development environment with Vagrant
How to get a list of links from a page from wikipedia
How to get a quadratic array of squares in a spiral!