I am trying to understand how to use a OnetoOne relation with unique_together. Currently trying to make use of them in my serializer and view with no success.
Models:
class Employees(models.Model):
name = models.CharField()
position = models.CharField()
phone = models.IntegerField()
class WorkSchedule(models.Model):
employee = models.OneToOneField('Employees')
workday = models.DateTimeField()
class Meta:
unique_together = (('employee', 'workday'),)
First question: Since the relation is OneToOne if i didn't have unique_together then each employee could appear in WorkSchedule once. However since i have unique_together this is not the case anymore. Correct?
Second Question: How would i use such models to return all employees and their workschedules like this
{
"employees": [
{
"employee": "John",
"workschedule": [
{
workday: "2022-03-03"
},
{
workday: "2022-03-04"
}
},
{
"employee": "Tom",
"workschedule": [
{
workday: "2022-03-03"
},
{
workday: "2022-03-04"
}
}
}
I was able to do this with using a ForeignKey(no unique_together) and a Nested Serializer however i wasn't able to do it with using the two models as they are above.