many to many. Подскажите как можно избавиться от if и вообще есть более простой вариант это реализовать?         def update(self, instance, validated_data):
                  
                  
                          tags = validated_data.get('tag')
                  
                  
                          mobile_codes = validated_data.get('mobile_code')
                  
                  
                          if tags:
                  
                  
                              tags = validated_data.pop('tag')
                  
                  
                              for tag in tags:
                  
                  
                                  current_tag, status = Tag.objects.get_or_create(
                  
                  
                                      **tag
                  
                  
                                  )
                  
                  
                                  ClientTag.objects.update(tag=current_tag)
                  
                  
                              return instance
                  
                  
                          if mobile_codes:
                  
                  
                              mobile_codes = validated_data.pop('mobile_code')
                  
                  
                              for mobile_code in mobile_codes:
                  
                  
                                  current_mobile_code, status = Tag.objects.get_or_create(
                  
                  
                                      **mobile_code
                  
                  
                                  )
                  
                  
                                  MailMobileCode.objects.update(mobile_code=current_mobile_code)
                  
                  
                              return instance
                  
                  
                          if tags and mobile_codes:
                  
                  
                              tags = validated_data.pop('tag')
                  
                  
                              for tag in tags:
                  
                  
                                  current_tag, status = Tag.objects.get_or_create(
                  
                  
                                      **tag
                  
                  
                                  )
                  
                  
                                  ClientTag.objects.update(tag=current_tag)
                  
                  
                              mobile_codes = validated_data.pop('mobile_code')
                  
                  
                              for mobile_code in mobile_codes:
                  
                  
                                  current_mobile_code, status = Tag.objects.get_or_create(
                  
                  
                                      **mobile_code
                  
                  
                                  )
                  
                  
                                  MailMobileCode.objects.update(mobile_code=current_mobile_code)
                  
                  
                              return instance
                  
                  
                          else:
                  
                  
                              return instance
                  
                  
                
Как это читать?
Обсуждают сегодня