Skip to content

Instantly share code, notes, and snippets.

View path_function.py
path('api/v1/people/<str:token>', …)
View url_function.py
url(r'^api/v1/people/(?P<token>[\w\-]+)/$', …)
View person_factory_batch_faker.py
import factory
from applications.people.models import Person
class PersonFactory(factory.Factory):
name = factory.Faker('name')
age = 25
class Meta:
View person_factory.py
import factory
from applications.people.models import Person
class PersonFactory(factory.DjangoModelFactory):
name = factory.Sequence(lambda n: 'John Doe{0}'.format(n))
age = 25
class Meta:
View fixture_example.json
[
{
"model": "applications.person",
"pk": 1,
"fields": {
"name": "Rick Sanchez",
"age": 70
}
},
{
View print_example_2.py
for person in Person.objects.all().prefetch_related('pets'):
print(person.name)
print('pets info:')
for pet in person.pets:
print(pet.name)
print('---')
View print_example_1.py
for person in Person.objects.all():
print(person.name)
print('pets info:')
for pet in person.pets.all():
print(pet.name)
print('---')
View over_18_example.py
from django.db import models
from applications.people.models import Person
class Pet(models.Model):
owner = models.ForeignKey(Person, related_name='pets', on_delete=models.CASCADE)
name = models.CharField(max_length=30)
View over_18_example.py
over_18_people = Person.objects.filter(age__gte=18)