Skip to content

FahadulShadhin/Django-ORM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django-ORM

Practice code while learning Django-ORM

Accessing shell

$ python manage.py shell

Import the model

>>> from <appname>.models import <modelname>

Creating objects

>>> from weblog.models import Blog
>>> b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
>>> b.save()

Retrieving all objects --> .all()

>>> q = Blog.objects.all()
>>> q
<QuerySet [<Blog: Beatles Blog>, <Blog: Django Blog>, <Blog: Python Blog>]>

Retrieving single object based on matched attribute --> .get(attribute='value')

>>> q = Blog.objects.get(name='Django Blog')
>>> q
<Blog: Django Blog>
>>> q = Blog.objects.get(tagline='All the latest Beatles news.')
>>> q
<Blog: Beatles Blog>

Return all items from a table that match a particular attribute value --> .filter(attribute='value')

>>> q = Blog.objects.filter(name__contains='Blog')
>>> q
<QuerySet [<Blog: Beatles Blog>, <Blog: Django Blog>, <Blog: Python Blog>]>
>>> q = Blog.objects.filter(name__startswith='Python')
>>> q
<QuerySet [<Blog: Python Blog>]>

############################################################
>>> 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

>>> q = Blog.objects.filter(name__startswith='Python') | Blog.objects.filter(name__startswith='Django')
>>> q
<QuerySet [<Blog: Django Blog>, <Blog: Python Blog>]>

About

Practice code while learning Django-ORM

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published