I have problem with creating object with recursive relation. So the scenario is right after create organization, insert user to just-created organization.
# models.py
class Organization(models.Model):
name = models.CharField(max_length=32)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
code = models.CharField(max_length=4, unique=True)
photo_path = models.CharField(max_length=256, null=True)
class Meta:
db_table = 'organization'
def __str__(self):
return self.name
class OrganizationLevel(models.Model):
organization = models.ForeignKey(
Organization,
on_delete=models.CASCADE,
db_index=False
)
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
db_index=False
)
name = models.CharField(max_length=48)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'organization_level'
unique_together = ('name', 'organization')
class OrganizationUnit(models.Model):
organization_level = models.ForeignKey(
OrganizationLevel,
on_delete=models.CASCADE,
db_index=False
)
name = models.CharField(max_length=48)
position = models.PointField(geography=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
db_index=False
)
address = models.CharField(max_length=256)
class Meta:
db_table = 'organization_unit'
unique_together = ('name', 'organization_level')
class User(models.Model):
email = models.CharField(max_length=64)
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
token = models.CharField(max_length=32, null=True)
tokenexp = models.DateTimeField(null=True)
photo_path = models.CharField(max_length=256)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
organization = models.ForeignKey(
Organization,
on_delete=models.CASCADE
)
is_activated = models.BooleanField(default=False)
code = models.CharField(max_length=32, null=True)
name = models.CharField(max_length=64)
birthdate = models.DateTimeField(null=True)
sex = models.CharField(max_length=1)
address = models.CharField(max_length=80)
organization_unit = models.ForeignKey(
OrganizationUnit,
on_delete=models.CASCADE
)
class Meta:
db_table = 'user'
So from given models, here's the flow:
- Create organization
- Create organization level instance from organization instance
- Create organization unit instance from organization level instance
I already try like this but got error
org = Organization.objects.create(
name=name,
code=code.upper()
)
org.save()
lvl = OrganizationLevel.objects.create(
organization=org,
parent=org.id,
name="Level1"
)
lvl.save()
unit = OrganizationUnit.objects.create(
name="Unit Name",
organization_level=lvl,
parent=lvl.id
)
unit.save()
Cannot assign "6": "OrganizationLevel.parent" must be a "OrganizationLevel" instance.
And what's the right answer?
orgis an Organization, butparentis a recursive relationship so needs to be an instance of OrganizationLevel. So why are you trying to passorg.idas the value?