Question regarding organizing models

71 views
Skip to first unread message

Bhavin Shah

unread,
Sep 23, 2021, 9:35:10 AM9/23/21
to Django users
Hello,

Somewhat new to Django. I am trying to figure out the best way to define/organize models in Django app which have interdependent foreign keys. Simplified example below.


Screenshot 2021-09-23 131449.png

The issue I am struggling with is the following
  1. The above example produces an error as User class is defined after Company. Reversing the order would cause the same issue with reference to Company class in User class. What is the best way to handle this scenario ? I tried to set the created_by_id value post the User class declaration, but then the django migrations do not recognize/set the foreign key correctly.
  2. I also tried to split the models into multiple files as the app is expected to have several models with such interdependent foreign keys. However in that scenario, the import are causing an error due to circular references.
Any suggestions/guidance on best way to handle such models would be very much appreciated. Thank you.

Regards,

Bhavin Shah

Avi shah

unread,
Sep 23, 2021, 10:09:39 AM9/23/21
to django...@googlegroups.com
create a third model and then use one-to-one relation to link the two tables with the third one so that its not a deadlock between the two

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e279e5da-55b5-4e7f-b6a3-3e3390b2fb63n%40googlegroups.com.

bnmng

unread,
Sep 23, 2021, 10:48:07 AM9/23/21
to Django users
Are you saying a user can only have one company?  If a user can have more than one company then just define the foreign key in company.  Then there will automatically be query set attached to each user called company_set.  Otherwise, you can define a OneToOneField in User.  You can create a third model as suggested, but I don't think you have to.  https://docs.djangoproject.com/en/3.2/topics/db/examples/one_to_one/

Adeyemi Deji

unread,
Sep 23, 2021, 4:32:23 PM9/23/21
to django...@googlegroups.com
Hi, what u are trying to do will produce an error when u run the code. When u use foreign keys between two models for example, only one of those models should ve the ForeignkeyField.ForeignKeyField acts like a bridge between two models or links two or more models. I advise u remove the company_id attribute from User class and move the User models to the top. It's makes sense to have a user who is capable of creating a company first before a company is eventually created. I need u to understand that while creating a model, u don't need to specify the AutoField as this will be generated for each model instance or object. I need u to also understand that when ever u create a model, think of a model as a parent that produces similar objects bcos u are dealing with Object Oriented Programming. So ask urself about what attributes should the parent have that after creating an object or objects those objects will also ve those attributes as well. Hope this helps

Screenshot 2021-09-23 131449.png

Peter of the Norse

unread,
Nov 14, 2021, 2:24:50 PM11/14/21
to django...@googlegroups.com
You can put a string with the name of a class that is yet to be defined in the ForeignKey.  https://docs.djangoproject.com/en/3.2/ref/models/fields/#foreignkey

In this case you would do

    created_by_id = models.ForeignKey("User", on_delete=models.DO_NOTHING)

On Sep 23, 2021, at 1:50 AM, Bhavin Shah <bhavinks....@gmail.com> wrote:

Hello,

Somewhat new to Django. I am trying to figure out the best way to define/organize models in Django app which have interdependent foreign keys. Simplified example below.


<Screenshot 2021-09-23 131449.png>

Twizerimana Emmanuel

unread,
Nov 18, 2021, 9:08:17 AM11/18/21
to django...@googlegroups.com
hy yooo
i got problem in python 
quadratic equation in python
how to operate delta that is less than 0 using complex function

--
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.
Reply all
Reply to author
Forward
0 new messages