You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Assume two models, class Parent(models.Model): and Child(Parent):
Assume they are both in the same Project
Can Child be in a different app than Parent? In most cases the answer seems to be yes.
Each app has many other models to which Parent and Child, respectively, are closely tied - which is why they are in those apps.
If so, what is the best practices way of doing so, and
how does one avoid or minimize circular imports?
For example, if Child.models.py imports Parent in order to facilitate the inheritance, what do you do if another model in Parent.models.py has a fk to another model in Child.models.py?
Mike Dewhirst
unread,
Sep 29, 2016, 9:25:11 PM9/29/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
On 30/09/2016 10:33 AM, Malik Rumi wrote:
> Assume two models, class Parent(models.Model): and Child(Parent):
>
> Assume they are both in the same Project
>
> Can Child be in a different app than Parent? In most cases the answer
> seems to be yes.
True. You only need to import Parent. But it might be better to put them
both in their own app with Parent defined before Child.
>
> Each app has many other models to which Parent and Child,
> respectively, are closely tied - which is why they are in those apps.
>
> If so, what is the best practices way of doing so, and
>
> how does one avoid or minimize circular imports?
In the other apps you don't need to import Parent and Child for FK and
such. That completely avoids circular imports. If your Parent and Child
classes are defined in the parentchild app then you specify your FK like
this ...
class ThisThing(models.Model):
parent = models.ForeignKey("parentchild.Parent")
...
class OtherThing(models.Model):
child = models.ForeignKey("parentchild.Child")
...
> For example, if Child.models.py imports Parent in order to facilitate
> the inheritance, what do you do if another model in Parent.models.py > has a fk to another model in Child.models.py?
You can also use quotes to specify the model class here. Just omit the
app name if they are in the same app.