Why does Django allow Circular Model Relation

128 views
Skip to first unread message

Eddilbert Macharia

unread,
Feb 19, 2016, 8:00:12 AM2/19/16
to Django users
Hello All,

I was working on a project, and realized that Django does not complain when you create Circular Model relationship even when creating migrations where i expected to have this complain

e.g.  the example does not make any sense as far as relationships go but it makes my point.


class User(models.Model):  
   
# ...
    product = models.ForeignKey("Product", on_delete=models.CASCADE)
   
class Products(models.Model):
    bought_buy
= models.ForeignKey(User, on_delete=models.CASCADE)

why is this or whats the reasoning behind Django team allowing this ?

Javier Guerra Giraldez

unread,
Feb 19, 2016, 8:42:52 AM2/19/16
to django...@googlegroups.com
On 19 February 2016 at 13:00, Eddilbert Macharia <edd....@gmail.com> wrote:
> Hello All,
>
> I was working on a project, and realized that Django does not complain when
> you create Circular Model relationship even when creating migrations where i
> expected to have this complain

why would it complain? it might be (sometimes) less than optimal, but
it's valid to have circular relationships.



--
Javier

Vadim Serdiuk

unread,
Feb 19, 2016, 8:48:20 AM2/19/16
to Django users
Why should it be complained? I know a lot of the same cases that may be applied.

пятница, 19 февраля 2016 г., 15:00:12 UTC+2 пользователь Eddilbert Macharia написал:

Eddilbert Macharia

unread,
Feb 19, 2016, 10:29:36 AM2/19/16
to Django users
Hello vadim when would such a relationship be needed I'm curious

Tim Chase

unread,
Feb 19, 2016, 11:08:27 AM2/19/16
to django...@googlegroups.com
On 2016-02-19 07:29, Eddilbert Macharia wrote:
> Hello vadim when would such a relationship be needed I'm curious

Think of a hierarchy at a company. Most employees have a
supervisor. So your table would reference the supervisor in the same
table:

class Employee(models.Model):
name = ...
# ...
supervisor = models.ForeignKey("Employee")

You can then query for a person's manager, as well as use the
reverse relationship to find out the employees that a person manages.

-tkc


Alex Heyden

unread,
Feb 19, 2016, 11:47:43 AM2/19/16
to django...@googlegroups.com
You would use it any time the relationship means something different. It would generally be inappropriate to use it as a simple backwards relationship (Django does this for you), but there's no reason not to have multiple relationships in a number of directions. In an HR system, someone could be both my beneficiary and my employee. 

On Fri, Feb 19, 2016 at 9:29 AM, Eddilbert Macharia <edd....@gmail.com> wrote:
Hello vadim when would such a relationship be needed I'm curious

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fbf2ef33-9309-44ce-aa73-f0238264589c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages