Issue
I have creating an app as my learning project in Django. There are 3 model classes:
# MOC class
class Moc(models.Model):
name = models.CharField(max_length=128, blank=True, null=True)
my other fields...
def __str__(self):
return str(self.id)
def save(self, *args, **kwargs):
created = not self.pk
super().save(*args, **kwargs)
if created:
CheckList.objects.create(moc=self)
# Pre Implement class
class CheckList(models.Model):
moc = models.OneToOneField(Moc, related_name='checklist', on_delete=models.CASCADE, default='1')
name = models.CharField(max_length=128, blank=True, null=True)
def __str__(self):
return str(self.id)
def save(self, *args, **kwargs):
created = not self.pk
super().save(*args, **kwargs)
if created:
CheckListItem.objects.create(checklist=self)
# Pre Implement Items class
class CheckListItem(models.Model):
checklist = models.ForeignKey(CheckList, related_name='checklistitems', on_delete=models.CASCADE, default='1')
action_item = models.TextField(max_length=128, blank=True, null=True)
actionee_name = models.ForeignKey(User, related_name='actionee_ready_pre_implements', on_delete=models.CASCADE, default='1')
action_due = models.DateField(blank=True, null=True)
def __str__(self):
return str(self.id)
I am creating Moc instance and on post save signal creating my CheckList class instance and consequently my CheckListItem class instances.
However, imaging that my CheckList once created always should have 10 CheckListItem objects as a pre populated list (like an initial data). I could not figure-out if this is something doable (at least how I am trying to achieve it as per my model relationships).
I do not want to hard code thus items in my HTML, I want to control add/delete of thus CheckListItems for related Moc/CheckList instances as relevant.
Any thoughts please?
Solution
I solved this by using InlineFormSets as it is responsible for FK relationships.
Answered By - SS_SS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.