Skip to content

Instantly share code, notes, and snippets.

View moacirmoda's full-sized avatar

Moacir Moda moacirmoda

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