This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try: | |
| Person.objects.get(id=1) | |
| except ObjectDoesNotExists: | |
| print('não existe') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for item in Person.objects.all().values('name'): | |
| print(item['name']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| queryset = Person.objects.all() | |
| for item in queryset: | |
| print(item.user.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Car(models.Model): | |
| RED = 'r' | |
| WHITE = 'w' | |
| BLUE = 'b' | |
| COLOR_CHOICES = ( | |
| (RED, 'red'), | |
| (WHITE, 'white'), | |
| (BLUE, 'blue'), | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person(models.Model): | |
| name = models.CharField(max_length=30) | |
| is_active = models.NullBooleanField() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person(models.Model): | |
| name = models.CharField(max_length=30) | |
| is_active = models.BooleanField(null=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Article(models.Model): | |
| reporter = models.OneToOneField(Reporter, on_delete=models.CASCADE) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Article(models.Model): | |
| reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE, unique=True) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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') |