I am importing Profile from profiles.models into meta.models, but showing the subject error. Can't find cutom solution, even though there are similar problems with different solutions out there but not working in my case.
Here is my profiles.models
from django.db import models
from django.contrib.auth.models import User
from meta.models import Designation
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, help_text = 'Foreign Key From User')
avatar = models.ImageField(upload_to='avatars', default='no_picture.png')
designation = models.ForeignKey(Designation, on_delete = models.CASCADE, null=True)
def __str__(self):
return str(self.user)
Here is my meta.models, where i am importing from profiles.models
from django.db import models
from profiles.models import Profile
class Designation(models.Model):
DesignationName = models.CharField(max_length = 20, blank=True,null= True)
DesignationScale = models.PositiveIntegerField()
def __str__(self):
return str(self.DesignationName)
class DutyPlaceSensitivity(models.Model):
sensitivity = models.CharField(max_length = 12, blank=True, null = True)
def __str__(self):
return str(self.sensitivity)
class DutyPlaces(models.Model):
DutyPlaceName = models.CharField(max_length =20, blank = True, null = True)
sensitivity = models.ForeignKey(DutyPlaceSensitivity, on_delete=models.CASCADE)
class ActingAs(models.Model):
acting_as_title = models.CharField(max_length = 12)
def __str__(self):
return str(self.acting_as_title)
class PlaceOfPosting(models.Model):
UserProfile = models.OneToOneField(Profile, on_delete=models.CASCADE)
acting_as = models.ForeignKey(ActingAs, on_delete = models.CASCADE)
def __str__(self):
return f"{self.UserProfile} - {self.acting_as}"
class TaskType(models.Model):
taskTypeName = models.CharField(max_length=20)
taskDescription = models.TextField(null = True, blank=True)
def __str__(self):
return str(self.taskTypeName)
class TaskPriority(models.Model):
priority_title = models.CharField(max_length = 12)
def __str__(self):
return str(self.priority_title)
class Tasks(models.Model):
task_name = models.CharField(max_length=120)
task_description = models.TextField()
task_type = models.ForeignKey(TaskType, on_delete=models.CASCADE)
assign_by = models.ManyToManyField(Profile, on_delete = models.CASCADE)
assign_to = models.ManyToManyField(Profile, on_delete = models.CASCADE)
task_priority = models.ForeignKey(TaskPriority, on_delete = models.CASCADE)
#time_stamp = models.DateField()
created = models.DateTimeField(auto_add_now = True)
updated = models.DateTimeField(auto_add = True)
def __str__(self):
return f"{self.task_name} --{self.assign_by}"
CodePudding user response:
To summarize the issues:
1/ To avoid cicular imports, you can use strings to declare foreign key target model, syntax: "app.Model".
class PlaceOfPosting(models.Model):
UserProfile = models.OneToOneField("profiles.Profile", on_delete=models.CASCADE)
2/ On_delete is not available on ManyToManyField since Django will handle the intermediate table for you.
3/ When a model has multiple FK or M2M on the same second model, related_name must be defined (https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey.related_name)