0

I have two models File and Shared, I want to add many collaborators to one File not one collaborators to many File, here are my models:

class File(models.Model):
    slug = models.SlugField(primary_key=True)
    file = models.FileField(upload_to=upload_file_to)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    collaborators = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL)


class Shared(models.Model):
    file = models.ForeignKey('File', on_delete=models.CASCADE)
    code = models.CharField(max_length=15, unique=True)

  • 1
    Then you need to add the ForeignKey to your user model, even though in this type of situations the relationships are more of M2M type. – Guillaume May 18 '21 at 00:28
  • Your title is wrong, Many-to-one is ForeignKey not One-to-many, you can refer to https://stackoverflow.com/questions/6928692/how-to-express-a-one-to-many-relationship-in-django – fitz Aug 02 '21 at 08:19

0 Answers0