Skip to content

Instantly share code, notes, and snippets.

Avatar

Moacir Moda moacirmoda

View GitHub Profile
View sample.py
for item in Person.objects.all().values('name'):
print(item['name'])
View sample.py
queryset = Person.objects.all()
for item in queryset:
print(item.user.name)
View sample.py
class Page(models.Model):
url = models.URLField(help_text="Example: '/about/contact/'. Make sure to have leading and trailing slashes.")
title = models.CharField(max_length=50)
content = models.TextField(help_text="Full HTML is allowed.")
View sample.py
class Car(models.Model):
RED = 'r'
WHITE = 'w'
BLUE = 'b'
COLOR_CHOICES = (
(RED, 'red'),
(WHITE, 'white'),
(BLUE, 'blue'),
)
View sample.py
class Person(models.Model):
name = models.CharField(max_length=30)
is_active = models.NullBooleanField()
View sample.py
class Person(models.Model):
name = models.CharField(max_length=30)
is_active = models.BooleanField(null=True)
View sample.py
class Article(models.Model):
reporter = models.OneToOneField(Reporter, on_delete=models.CASCADE)
View sample.py
class Article(models.Model):
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, unique=True)
View sample.py
class Reporter(models.Model):
# ...
pass
class Article(models.Model):
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, related_query_name='author')
>>> Article.objects.filter(author__name='Moacir')
View sample.py
>>> Article.objects.filter(reporter__name='Moacir')
You can’t perform that action at this time.