Advertisement
Guest User

Untitled

a guest
Jul 13th, 2020
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. class ProductImageSerializer(serializers.ModelSerializer):                      
  2.     1     violated = serializers.SerializerMethodField()                              
  3.     2                                                                                
  4.     3     def get_violated(self, obj):                                                
  5.     4         return obj.found_copyright_violations.exists()  # if there are violations
  6.     5                                                                                
  7.     6     class Meta:                                                                
  8.     7         model = ProductImage                                                    
  9.     8         fields = ('product', 'image_id', 'url', 'selected', 'violated')        
  10.     9         # otherwise, there will be validation errors                            
  11.    10         extra_kwargs = {"image_id": {"validators": []}}                        
  12.    11                                                                                
  13.    12                                                                                
  14.    13 class ProductSerializer(serializers.ModelSerializer):                          
  15.    14     images = ProductImageSerializer(many=True)                                  
  16.    15                                                                                
  17.    16     class Meta:                                                                
  18.    17         model = Product                                                        
  19.    18         fields = ('product_id', 'title', 'images', 'active')                    
  20.    19                                                                                
  21.    20     def update(self, instance, validated_data):                                
  22.    21         # Updating images selected field                                        
  23.    22         for image_data in validated_data.get("images"):                        
  24. >> 23             image_instance = ProductImage.objects.get(                          
  25.    24                 pk=image_data.get("image_id"),                                  
  26.    25             )                                                                  
  27.    26                                                                                
  28.    27             image_instance.selected = image_data.get("selected")                
  29.    28             image_instance.save()                                              
  30.    29                                                                                
  31.    30         selected_images = instance.images.filter(selected=True)                
  32.    31                                                                                
  33.    32         # If there are selected images, set to either true or false.            
  34.    33         if selected_images:                                                    
  35.    34             instance.active = validated_data.get('active', instance.active)    
  36.    35         # Otherwise false                                                      
  37.    36         else:                                                                  
  38.    37             instance.active = False                                            
  39.    38                                                                                
  40.    39         instance.save()                                                        
  41.    40                                                                                
  42.    41         return instance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement