[Answered ]-Django user relationship

1👍

Use inheritance: define a superclass for Company, with the common fields, and then inherit that class and add the stuff ClassACompany and ClassBCompany need.

This way the UserProfile can have a foreign key to Company. If you need to get from the company to the specific type of company, you can do that as described in the docs.

1👍

why dont you just have one class for Company? that’ll make your system much, much simpler.

you can then have specific fields inside Company that will let you determine whether it’s of type A or B (what’s the difference anyway?)

0👍

If you really must have different fields inside CompanyA and CompanyB then you can have them both derive from a common Company class which your ForeignKey will point to.

0👍

You would need to reference the Company model, and if need be, subclass Company with CompanyA and CompanyB. For simplicity, your Company class could have a type attribute with possible A and B values, then you could avoid subclassing.

👤styts

Leave a comment