K
K
kaato2016-04-14 10:56:02
Django
kaato, 2016-04-14 10:56:02

Do I need to create a separate URL to register a "Company" along with its "Owner"?

The problem is this - there are two models User and Company. The URLs .../api/users and .../api/companies/ are attached to them, respectively. One company has many users and one of them, logically, is its owner (the user has a ForeignKey company indicating the company to which he belongs, as well as a role attribute that tells what position he holds there). The requirements are such that the user cannot belong to any company. This was solved using the .../companies//users/ edgepoint, from which you can create users who will immediately belong to a particular company. But the company also cannot be without an owner, and it is no longer possible to create ownerless users. Therefore, it is required to create a company and the owner of this company at the same time, that is, when registering a company. And here the question arises - is it possible to create a user together with the company in the POST .../api/companies/ request and simply ask to send user data through options, or even create a separate URL for registering these two. The first option, in my opinion, completely breaks the idea of ​​a RESTful API, although I may be wrong since I'm new to REST, but the idea that the endpoint responsible for creating companies now also takes responsibility for creating users jars me .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iegor, 2016-04-14
@kaato

If the User is connected to the Company via a foreign key, then if the Company does not exist, then there is no user, so when we send a post to .../api/companies/ we need to create a user, which means the first option is not bad. True, this is all reasonable only if the user does not have the opportunity to change the company

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question