Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nahasco opened this issue Aug 8, 2022 · 4 comments
Closed

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

nahasco opened this issue Aug 8, 2022 · 4 comments

Comments

@nahasco
Copy link

nahasco commented Aug 8, 2022

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
Copy link
Member

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
Copy link
Author

nahasco commented Oct 14, 2022

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
Copy link
Member

fdintino commented Oct 14, 2022

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants