-
Notifications
You must be signed in to change notification settings - Fork 97
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
Comments
You can determine the subject by following the foreign key from |
How can I do that?
I expect the same behavior implemented with subject - section. The current behavior is'nt efficient nor intuitive. Thanks. |
The 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.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. |
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. |
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.
The text was updated successfully, but these errors were encountered: