Search code examples
pythondjangodjango-modelsdjango-2.1

Add multiple users to model


I am creating a page where only a certain users can access, I would like to add them manualy, at the moment here is what I have

models.py

class Hotel(models.Model):
manager = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name="Manager d'équipe")
code = models.CharField(max_length=500,verbose_name="Code hôtel", default="N/A")
contact_rh = models.EmailField(max_length=150,verbose_name="Contact RH", default="N/A")
contact_gm = models.EmailField(max_length=150,verbose_name="Contact GM", default="N/A")
payday = models.CharField(max_length=500,verbose_name="Jour de paye prévu du mois")
hotel = models.CharField(max_length=500,verbose_name="Nom de l'hôtel")
planning = models.ImageField(default='default.jpg', upload_to='Planning mois en cours')

def __str__(self):
    return self.hotel

But of course I cannot add more than one user I cannot figured out.

Thanks


Solution

  • Please see previous answers, for that to work I had to change ForeignKey to ManyToManyField and for my purpose so only the users added can see the page I had to add the folowing code to my list views.py :

    def get_queryset(self):
        return self.model.objects.filter(collaborateurs=self.request.user)