theatlantic / django-nested-admin

Django admin classes that allow for nested inlines
http://django-nested-admin.readthedocs.org/
Other
715 stars 99 forks source link

Nested inlines do not inherit top level foreign key values #224

Closed nahasco closed 1 year ago

nahasco commented 2 years ago

So I have 3 nested models

Subjects > Sections > Topics

This package is what I need to present all of these in one single admin page and in my case it is the Subject page.

Inside subjects, sections dont ask for "subject" anymore and inherits it automatically. Inside sections that are inside subjects are the topics. Topics dont ask for sections that's are above them which is good but they do ask for "subject" which is one more level above.

Basically inlines are automatically inheriting foreign key values one level above only and not two or more. I hope I am making sense here and I think the screenshot will explain it better.

Screen Shot 2022-08-08 at 11 35 45 PM
fdintino commented 1 year ago

You can determine the subject by following the foreign key from topic.section.subject, so isn't it redundant to also have a foreign key from topic to subject? Also, I don't really understand what it would mean to "inherit" the value in this instance...if you changed the subject title would you also expect the subject dropdown to automatically update? What about when you are editing a new subject that hasn't been saved yet?

nahasco commented 1 year ago

You can determine the subject by following the foreign key from topic.section.subject

How can I do that?

Also, I don't really understand what it would mean to "inherit" the value in this instance...if you changed the subject title would you also expect the subject dropdown to automatically update? What about when you are editing a new subject that hasn't been saved yet?

I expect the same behavior implemented with subject - section. The current behavior is'nt efficient nor intuitive.

Thanks.

fdintino commented 1 year ago

The subject field doesn't show up for sections because that foreign key is the inline. Similarly, the foreign key from topic to section is what makes those inlines; there isn't a "Section" dropdown for each topic. It wouldn't even make sense, because the user isn't choosing the section, it happens automatically. I imagine your models look something like this:

class Subject(models.Model):
    title = models.CharField(max_length=255)

    def __str__(self):
        return self.title

class Section(models.Model):
    position = models.PositiveIntegerField()
    title = models.CharField(max_length=255)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ("position",)

class Topic(models.Model):
    position = models.PositiveIntegerField()
    title = models.CharField(max_length=255)
    section = models.ForeignKey(Section, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ("position",)

Now, suppose you have an instance of Section

section = Section.objects.get(title="Algebra")

You can get the subject with the foreign key:

print(section.subject)  # prints "Math"

The same is true for the topic; you can get the section from the foreign key.

topic = Topic.objects.get(title="Solving equations")
print(topic.section)  # prints "Algebra"

And since we've already established that you can get the subject from the section, you can put them both together

print(topic.section.subject)  # prints "Math"

so you see, you don't need a ForeignKey from topic to subject. In fact, you shouldn't have one. It would be redundant because a relationship already exists. You just have to go through the Section to get to it.

fdintino commented 1 year ago

I'm closing this for now, since I'm not sure there's an issue here. If I'm wrong please feel free to reopen.