Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View moacirmoda's full-sized avatar

Moacir Moda moacirmoda

View GitHub Profile
try:
Person.objects.get(id=1)
except ObjectDoesNotExists:
print('não existe')
for item in Person.objects.all().values('name'):
print(item['name'])
queryset = Person.objects.all()
for item in queryset:
print(item.user.name)
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.")
class Car(models.Model):
RED = 'r'
WHITE = 'w'
BLUE = 'b'
COLOR_CHOICES = (
(RED, 'red'),
(WHITE, 'white'),
(BLUE, 'blue'),
)
class Person(models.Model):
name = models.CharField(max_length=30)
is_active = models.NullBooleanField()
class Person(models.Model):
name = models.CharField(max_length=30)
is_active = models.BooleanField(null=True)
class Article(models.Model):
reporter = models.OneToOneField(Reporter, on_delete=models.CASCADE)
class Article(models.Model):
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, unique=True)
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')