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/

On Thursday, September 23, 2021 at 10:09:39 AM UTC-4 get...@gmail.com wrote:

> 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
>
> On Thu, Sep 23, 2021 at 7:05 PM 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.
>>
>>
>> [image: 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
>>
>> -- 
>> 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
>>  
>> <https://groups.google.com/d/msgid/django-users/e279e5da-55b5-4e7f-b6a3-3e3390b2fb63n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fb6f757d-68fd-4d0c-a860-c69dfc41e42cn%40googlegroups.com.

Reply via email to