Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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
, ]>
```