Django + React: Shared Model Consistency

49 views
Skip to first unread message

Guilherme Leal

unread,
Jul 5, 2017, 2:18:33 PM7/5/17
to django...@googlegroups.com
I'm starting a new project that uses Django (with DRF) as backend and React as frontend but I have some concerns about data consistency across what the API endpoints might expect, and what the client with React could provide.

Exemple:

Lets say I have the following model:

class Person(models.Model):
    name = models.CharField(max_length=250)
    gender = models.IntegerField(choices=MY_GENDER_CHOICES)

If one would build an API endpoint for exclusively this model, the endpoint would answer with the fields 'name' and 'gender' everytime it would be "GETed", or would accept only the fields 'name' and 'gender' when "POSTed'. Furthermore, in both cases the 'gender' field have a limited set of values it can answer/accept.

The concern I was talking about is HOW THE HELL am I going to tell the client that he only can use "this" or "that" limited set of fields or values? Even more, I want to teach the client how to discover this info, or simply put, share model specification between "client" and "server".

One thing to note: I did looked over React+Relay+GraphQL. The features are almost exactly what I was looking for, but I did find it really really really overcomplex for the size of the project I have in mind.

Anyone have anything in mind to help or sujest?

George Silva

unread,
Jul 5, 2017, 2:38:47 PM7/5/17
to django-users
Hello Guilherme,

The easy way is provide an endpoint with valid values for that relation. Other than that, the client doesn't need to know about that. He can post anything and you can answer back with the errors.

If he posts, for example, an invalid gender, you can validate that in your DRF and just spit out the errors for the client.

But there is no other way (that I can see) to let the client configure itself, without having a valid values list for that.

--
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+unsubscribe@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/CAOs3Lp6cp8vm403D0nWd%2B09-PDZKZQOcaBcU2dvNi7LUA--%2BeA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
George R. C. Silva
Sigma Geosistemas LTDA
----------------------------

Guilherme Leal

unread,
Jul 5, 2017, 2:47:46 PM7/5/17
to django...@googlegroups.com
>>The easy way is provide an endpoint with valid values for that relation. 

That ideia crossed my mind more than once, but that adds at least 1 more api call (which already tends to be numerous in most cases), and while I can, will avoid that route.

>>Other than that, the client doesn't need to know about that. He can post anything and you can answer back with the errors.

The "problem in the equation" is not the relation. The problem I'm trying to avoid is exactly the "client post anything and get an error (or a list of errors) as answer". That is not productive and really meaninfull for the user experience (in a bad way).

I was trying to maybe write a django app that got the model specification, and exported some js interface to some output folder. Its basicaly the same ideia as the "types" from GraphQL.

Alceu Rodrigues de Freitas Junior

unread,
Jul 5, 2017, 3:19:53 PM7/5/17
to django...@googlegroups.com

Never used Django REST Framework (DRF) before, but I guess you're trying to achieve something that should be handled by your API documentation.

It crossed my mind that you're looking for describing the payload your REST API expects and that should be done dinamically by the consumer. Never saw something like that for REST as exists for SOAP, but looks like somebody already spent time on that:

https://www.ibm.com/developerworks/webservices/library/ws-restwsdl/

Never used though, you might want to experiment with it.

Besides that, some custom exception explaining the problem when wrong values were provided should help (at least the developers trying to use your API).

--
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.

Guilherme Leal

unread,
Jul 5, 2017, 3:35:55 PM7/5/17
to django...@googlegroups.com
Alceu, thanks for the idea.

DRF already have (something like) that [1]. Like I said to George, I'm trying to avoid the "request the metadata from the api call" aproach, as it tends to bloat the client with metadata requests. I'm looking for something lke a design pattern or a development tool.

But your ideia was the best so far.


----

2017-07-05 16:19 GMT-03:00 Alceu Rodrigues de Freitas Junior <alceu.fr...@gmail.com>:

Never used Django REST Framework (DRF) before, but I guess you're trying to achieve something that should be handled by your API documentation.

It crossed my mind that you're looking for describing the payload your REST API expects and that should be done dinamically by the consumer. Never saw something like that for REST as exists for SOAP, but looks like somebody already spent time on that:

https://www.ibm.com/developerworks/webservices/library/ws-restwsdl/

Never used though, you might want to experiment with it.

Besides that, some custom exception explaining the problem when wrong values were provided should help (at least the developers trying to use your API).


Em 05/07/2017 15:17, Guilherme Leal escreveu:
I'm starting a new project that uses Django (with DRF) as backend and React as frontend but I have some concerns about data consistency across what the API endpoints might expect, and what the client with React could provide.

Exemple:

Lets say I have the following model:

class Person(models.Model):
    name = models.CharField(max_length=250)
    gender = models.IntegerField(choices=MY_GENDER_CHOICES)

If one would build an API endpoint for exclusively this model, the endpoint would answer with the fields 'name' and 'gender' everytime it would be "GETed", or would accept only the fields 'name' and 'gender' when "POSTed'. Furthermore, in both cases the 'gender' field have a limited set of values it can answer/accept.

The concern I was talking about is HOW THE HELL am I going to tell the client that he only can use "this" or "that" limited set of fields or values? Even more, I want to teach the client how to discover this info, or simply put, share model specification between "client" and "server".

One thing to note: I did looked over React+Relay+GraphQL. The features are almost exactly what I was looking for, but I did find it really really really overcomplex for the size of the project I have in mind.

Anyone have anything in mind to help or sujest?
--
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+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jul 6, 2017, 5:18:46 AM7/6/17
to django...@googlegroups.com

On Wednesday 05 July 2017 16:34:49 Guilherme Leal wrote:

 

> DRF already have (something like) that [1]. Like I said to George, I'm

> trying to avoid the "request the metadata from the api call" aproach,

> as it tends to bloat the client with metadata requests.

 

There are no psychic clients available yet. Somewhere the client needs to be taught what is considered valid. You always need at least one request (complex, but limits meta data requests) to expose your API's parameters. The other way is to implement some lightweight protocol per endpoint - this is preferable if your API is large and clients typically use a small subset. A structured machine readable error response is the common way. But one can also use HTTP's OPTION method to do this.

 

While OPTION methods shall not be cacheable, nothing prevents you from implementing a versioning scheme and sending an api version header with each response. If the version deviates, the client knows to request the OPTION header again for future use.

 

Either way, you require at least one request to start the learning process.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages