简体   繁体   中英

How to make it possible to add more than one form for a user

I am having trouble creating a model for user availability

I already have something like this

models.py

class Availability(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    date = models.DateField(null=True)
    starting_hour = models.TimeField(auto_now_add=False, auto_now=False, null=True)
    ending_hour = models.TimeField(auto_now=False, auto_now_add=False, null=True)

    def __str__(self):
        return str(self.user)

When a user tries to add an availability for another day of the week, a message appears that the availability for that user already exists I would appreciate any help

You have a One to One relation between User and Availability, change it to a foreign key:

class Availability(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    date = models.DateField(null=True)
    starting_hour = models.TimeField(auto_now_add=False, auto_now=False, null=True)
    ending_hour = models.TimeField(auto_now=False, auto_now_add=False, null=True)

    def __str__(self):
        return str(self.user)

A One to One relation means that there can only be one object of User for Availability and vice-versa. Making this change may also make it such that getting availability objects would now be different, previously this would have been fine user.availability now you would need user.availability_set.all()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM