from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Board(models.Model):
title = models.CharField(max_length=50, null=True)
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
def __str__(self):
return self.title
class Task(models.Model):
title = models.CharField(max_length=200, null=True)
done = models.BooleanField(default=False, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
board = models.ForeignKey(Board, null=True, on_delete=models.CASCADE)
def __str__(self):
return self.title
how can I get all tasks that are inside one board? (every user can create a board and inside that board the user can create tasks) I've tried Board.objects.get(pk=1).title.title
but that doesn't seem to work.
You can retrieve the Board
object, and then query with task_set
:
board = Board.objects.get(pk=1)
board.task_set.all() # queryset of related Tasks
If you are not interested in the Board
itself, you can omit querying the Board
, and filter with:
Task.objects.filter(board_id=1) # queryset of related Tasks