1

I want to make a task model and a user model. And I want each task to be able to be related to 3 users. Each task should be related to a creator user, an assignee user, and a verifier user. And I want to only have one user table. My inclination is to have 3 foreign keys on the task table: creator_id, assignee_id, and verifier_id. Is this the correct way to do it? How do I model that in Django?

UPDATE

Here's my simplified models

class User(models.Model):
    id = models.CharField(primary_key=True, max_length=100)
    name = models.CharField(max_length=100)

class Task(models.Model):
    id = models.CharField(primary_key=True, max_length=100)
    title = models.CharField(max_length=100)

    creator = models.ForeignKey('User', on_delete=models.DO_NOTHING, related_name='creator_tasks')
    assignee = models.ForeignKey('User', on_delete=models.DO_NOTHING, related_name='user_tasks')
    verifier = models.ForeignKey('User', on_delete=models.DO_NOTHING, related_name='verifier_tasks')

And here's the error I get when I relate a user to a task as a creator and try to get the task's creator:

$ python manage.py shell
>>> from todoapp.models import User
>>> from todoapp.models import Task
>>> user = User.objects.get(id='1')
>>> task = Task.objects.get(id='1')
>>> task.creator_id = user.id
>>> task.save()
>>> task.creator
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/josh/.venv/myenv/lib/python3.9/site-packages/django/db/models/fields/related_descriptors.py", line 188, in __get__
    raise self.RelatedObjectDoesNotExist(
todoapp.models.Task.creator.RelatedObjectDoesNotExist: Task has no creator.
1
  • I'm not getting that error when I try to replicate it. How were your User and Task objects created? Commented Oct 25 at 22:58

2 Answers 2

2

Try something like :

task.creator = user
task.save()

...and so on for an assignee and a verifier

This automatically manages the foreign key relationship properly.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the trick. Thank you
0

Use 3 Foreignkey on Task model and use related_name to identify each of creator, assigner and verifier

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
That's exactly what I thought I should do. However, I'm getting an error. It doesn't think a Task has a relation to Creator. I added my code. Do I have it set up right? Also, do the values I'm giving for related_name make sense? Can you suggest better if not? Am I setting creator correctly in my shell session and calling creator correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.