2

Suppose I have the following two models task and person. Now each person can have multiple tasks (one to many relationship).

Now in the admin app for a person I can only add one foreign key per record thus I'll have to create multiple records for a person having a different task but the same email and name fields.

Is there anything I can do in personAdmin that would allow me to add multiple foreign keys for the same person and in the backend it would create multiple records ?

class task(models.Model):
   description = models.CharField(max_length=100)

class person(models.Model):
   task  = models.ForeignKey(Author, on_delete=models.CASCADE)
   email =models.CharField(max_length=100) 
   name  = models.CharField(max_length=100)

class personAdmin(admin.ModelAdmin):
      pass
James Franco
  • 4,516
  • 10
  • 38
  • 80

1 Answers1

0

You could add a third table with both the first two tables as FOREIGN KEY.

So this is what you have currently

class ToDo_Task(models.Model):
   description = models.CharField(max_length=100)

class User(models.Model):
   task  = models.ForeignKey(Author, on_delete=models.CASCADE)
   email =models.CharField(max_length=100) 
   name  = models.CharField(max_length=100)

Adding a third table

class person_task(models.Model):
    task = models.ForeignKey(ToDo_Task, on_delete = models.CASCADE)
    person = models.ForeignKey(User, on_delete = models.CASCADE)
    ...

You may add even more fields in the new table.

Amit Kumar
  • 804
  • 2
  • 11
  • 16