mirror of
				https://github.com/fastapi-users/fastapi-users.git
				synced 2025-11-04 06:37:51 +08:00 
			
		
		
		
	Create a user programmatically documentation - change example to return things. (#1356)
* Changed method to return user or raise an exception * Re-raise UserAlreadyExists exception instead of creating a new one.
This commit is contained in:
		@ -30,7 +30,7 @@ In the following sample, we import our dependencies and create a context manager
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
We are now ready to write a function. The example below shows you a basic example but you can of course adapt it to your own needs. The key part here is once again to **take care of opening every context managers and pass them every required arguments**, as the dependency manager would do.
 | 
					We are now ready to write a function. The example below shows you a basic example but you can of course adapt it to your own needs. The key part here is once again to **take care of opening every context managers and pass them every required arguments**, as the dependency manager would do.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```py hl_lines="13-25"
 | 
					```py hl_lines="13-27"
 | 
				
			||||||
--8<-- "docs/src/cookbook_create_user_programmatically.py"
 | 
					--8<-- "docs/src/cookbook_create_user_programmatically.py"
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -21,5 +21,7 @@ async def create_user(email: str, password: str, is_superuser: bool = False):
 | 
				
			|||||||
                        )
 | 
					                        )
 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
                    print(f"User created {user}")
 | 
					                    print(f"User created {user}")
 | 
				
			||||||
 | 
					                    return user
 | 
				
			||||||
    except UserAlreadyExists:
 | 
					    except UserAlreadyExists:
 | 
				
			||||||
        print(f"User {email} already exists")
 | 
					        print(f"User {email} already exists")
 | 
				
			||||||
 | 
					        raise
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user