0

let's say i have these classes :

models.py

class Parent(models.Model):
    title = models.CharField(max_length=250)


class Child(models.Model):
    parent = models.ForeignKey(Parent)
    title = models.CharField(max_length=250)


class Family(models.Model):
    title = models.CharField(max_length=250)
    parent = models.ForeignKey(Parent)
    child = models.ManyToManyField(Child)

the problem with this code the family form shows all the "Child" objects.

I need to show "Child" objects only if the Child is related to "Parent" objects in the family form.

if there's a way not to use manytomanyfield i am open to that too..

any idea?

DjangoGuy
  • 103
  • 2
  • 13

1 Answers1

0

Maybe this solution can help!

class Parent(models.Model):
        title = models.CharField(max_length=250)

class Child(models.Model):
    parent = models.ForeignKey(Parent)
    title = models.CharField(max_length=250)

class Family(models.Model):
    title = models.CharField(max_length=250)
    parent = models.ForeignKey(Parent)

    filtered_childs = Child.objects.all().filter(parent=self.parent)
    filtered_childs = list((k, v) for k, v in enumerate(filtered_childs))
    child = models.CharField(choices=filtered_childs, max_length=20)
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64