ob-vaults/Super_Vault/200_programming/python/django/django admin.md
2024-09-12 17:54:01 +03:30

105 lines
1.4 KiB
Markdown

status: #doc
Tags: #python/django
links:
Date: 2024-08-25
___
# setup
for access run server and go to 127.0.0.1:8000/admin or yoursite.com/admin
## making superuser
run command and type needed fields
```sh
python manage.py createsuperuser
```
# models manipulation
## registering models
go to admin.py file inside my_app folder and register your models like this
```python
from .models import person
admin.site.register(person)
```
## string method
you see that every model object is show like "person object 1" we don't want that so for changing how it is showing in admin go to models.py file and add `__str__` function like this
```python
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
def __str__ (self):
return f"{self.firstname} {self.lastname}"
```
## add , delete , update via admin
for making changes click on person model inside admin so it show table of objects
- add : you can click on `+add person` to add another object you just fill the form and click on save
- update : click on object you want , change the details and add click save
- delete : select object you want and form drop down on top click `delete selected person` add click go it show the changes if you want click on `yes,i'm sure`
---
# References