Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fahadulshadhin/django-orm
Practice code while learning Django-ORM
https://github.com/fahadulshadhin/django-orm
django orm queryset
Last synced: 3 days ago
JSON representation
Practice code while learning Django-ORM
- Host: GitHub
- URL: https://github.com/fahadulshadhin/django-orm
- Owner: FahadulShadhin
- Created: 2021-12-08T13:55:18.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-09T17:19:32.000Z (about 3 years ago)
- Last Synced: 2024-11-14T16:45:42.786Z (2 months ago)
- Topics: django, orm, queryset
- Language: Python
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Django-ORM
Practice code while learning Django-ORM### Accessing shell
```Shell
$ python manage.py shell
```
### Import the model
```Pyhton
>>> from .models import
```### Creating objects
```Python
>>> from weblog.models import Blog
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
>>> b.save()
```### Retrieving all objects --> .all()
```Python
>>> q = Blog.objects.all()
>>> q
, , ]>
```### Retrieving single object based on matched attribute --> .get(attribute='value')
```Python
>>> q = Blog.objects.get(name='Django Blog')
>>> q>>> q = Blog.objects.get(tagline='All the latest Beatles news.')
>>> q```
### Return all items from a table that match a particular attribute value --> .filter(attribute='value')
```Python
>>> q = Blog.objects.filter(name__contains='Blog')
>>> q
, , ]>
>>> q = Blog.objects.filter(name__startswith='Python')
>>> q
]>############################################################
>>> ModelName.objects.filter(attribute='value')
>>> ModelName.objects.filter(attribute__startswith='value')
>>> ModelName.objects.filter(attribute__contains='value')
>>> ModelName.objects.filter(attribute__icontains='value')
>>> ModelName.objects.filter(attribute__gt='value')
>>> ModelName.objects.filter(attribute__gte='value')
>>> ModelName.objects.filter(attribute__lt='value')
>>> ModelName.objects.filter(attribute__lte='value')
############################################################
```### OR Query
```Python
>>> q = Blog.objects.filter(name__startswith='Python') | Blog.objects.filter(name__startswith='Django')
>>> q
, ]>
```