python - Django Rest Framework ModelSerializer Set attribute on create -


When creating an object I initially use the logged-in user to specify the model field 'owner' I am

Model:

  class account (models.Model): id = models.AutoField (primary_key = True) owner = model. Forwardkey (user) name = model.carfield (max_length = 32, exclusive = true) Description = Model.Carfild (Max_Langu = 250, empty = True)   

To set owner Serializer:

  Class Account Operator ModelSerializer): Class Meta: Model = Model. Account field = ('name', 'description') def restore_object (self, attrs, example = none): example = super (). Restore_object (attrs, example) Request = self .context.get ('request', none) Setter (example, 'owner', request.user) Example of return   

In my system It is possible to update the account of another for a different user, but the original user should be owned. Obviously the above breaks down because with the currently logged in user the ownership over the update will be overwritten.

So I updated it like this:

  class AccountSerializer (serializers) New_instance = False if not an example: new_instance = true example = super (). Models: Model Meta: model = models.Account fields = ('name', 'description') def restore_object (self, attrs, instance = none) .restore_object (attrs, instance) # set the owner only if this is a new example If new_instance: request = self.context.get ('request', none) setter (e.g., 'owner', request.user) Example of return   

Does it do something like Recommended method? I can not see any other way, but I still have very limited experience.

Thanks

From the review of @zaphod100.10 Alternatively, in view code (removed with custom restore_object method in the above serializer):

  def post (self, request, * args, ** kwargs): serializer = self.get_serializer (Data = Request.DATA, files = request.FILES) if serializer.is_valid (): serializer.object.owner = request.user self.pre_save (serializer.object) self.object = serializer.save (force_insert = true) self .post_save (Reaction (Serializer, errors, status = status. HTTPPADAADADADASACADA) Returns (Serializer data, status = status. TTP_201, answer = headers) header = self-code>   

Actually you set up the owner on creation I do not want to be on the subsequent update, for that I think that you should set up the owner in the post view, I think that this is a more logical and robust way, the update is done through a foot scene so that your data Should always be right because there is no way to update If the owner is not editable at PUT.

To create ideas, you were seen based on the general category of DRF. Use retrieveUpdateDeleteView as it is. Override the post method for ListCreateView Use the Django model form to validate the data and create account frequency.

You must copy the request. Enter 'DATA dict' and 'Owner' as an existing user.

The code for POST method can be:

  def post (self, request, * args, ** kwargs): data = dipkopy (request data) data [ 'Owner'] = request.user form = accountform (data = data) if form.is_valid (): example = form.save (commit = false) instance.save () return response (dict (id = instance.pk), Status = Status.HTTP_201_ CREATED) Return Feedback (Forms Errors, Status = Status .tttpdmADADADADCRCD)    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -